Java Reference
In-Depth Information
Human-Scale Time
In the previous section, I discussed the use of
Instant
and
Duration
classes whose instances are more suited to
dealing with machine-scale time. Humans deal with time in terms of fields such as year, month, day, hour, minute,
and second. Recall the following format for specifying date and time in ISO-8601 format:
[date]T[time][zone offset]
The Date-Time API provides several classes, listed in Table
12-2
, to represent all fields and their combinations of
human-scale time. A Yes or No in the component column for a class indicates whether an instance of the class stores
that component or not. I will discuss all of these classes in detail shortly.
Table 12-2.
Human-Scale Date and Time Classes and their Components
Class Name
Date
Time
Zone Offset
Zone Rule
LocalDate
Yes
No
No
No
LocalTime
No
Yes
No
No
LocalDateTime
Yes
Yes
No
No
OffsetTime
No
Yes
Yes
No
OffsetDateTime
Yes
Yes
Yes
No
ZonedDateTime
Yes
Yes
Yes
Yes
ZoneOffset
No
No
Yes
No
ZoneId
No
No
Yes
Yes
The ZoneOffset Class
An instance of the
ZoneOffset
class represents a fixed zone offset from UTC time zone, for example, +05:30, -06:00, etc.
It is a period of time that a time zone differs from the UTC.
A
ZoneOffset
is not aware of the changes in zone offset because of the observed Daylight Saving Time.
The
ZoneOffset
class declares three constants:
•
UTC
MAX
•
MIN
UTC
is the time zone offset constant for UTC.
MAX
and
MIN
are the maximum and minimum supported zone offsets.
•
■
Tip
Z, not +00:00 or -00:00, is used as the zone offset designator for UtC time zone.
The
ZoneOffset
class provides methods to create its instances using a combination of hour, minute, and second.
Listing 12-3 demonstrates how to create instances of the
ZoneOffset
class.