Differences between Periods and Durations
Table 17.4 summarizes the differences between selected methods of the Period and the Duration classes, mainly in regard to the temporal units supported, representation for parsing and formatting, and comparison. N/A stands for Not Applicable.
Table 17.4 Some Differences between the Period Class and the Duration Class
Methods | The Period class | The Duration class |
of(amount, unit) | N/A | Valid ChronoUnits: NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS (p. 1065). |
parse(text) toString() | Representation based on: PnYnMnD and PnW (p. 1057). | Representation based on: PnDTnHnMn.nS (p. 1065). |
get(unit) | Supported ChronoUnits: YEARS, MONTHS, DAYS (p. 1059). | Supported ChronoUnits: NANOS, SECONDS (p. 1067). |
getUnits() | Supported ChronoUnits: YEARS, MONTHS, DAYS (p. 1059). | Supported ChronoUnits: NANOS, SECONDS (p. 1067). |
equals(other) | Based on values of individual units (p. 1059). | Based on total length (p. 1067). |
compareTo(other) | N/A | Natural order: total length (p. 1067). |
minus(amount, unit) plus(amount, unit) | N/A | Valid ChronoUnits: NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, HALF_DAYS, DAYS (p. 1069). |
abs() | N/A | Returns copy with positive length (p. 1069). |
dividedBy(divisor) | N/A | Returns copy after dividing by divisor (p. 1069). |
normalized() | Only years and months normalized (p. 1061). | N/A |
17.7 Working with Time Zones and Daylight Savings
The following three classes in the java.time package are important when dealing with date and time in different time zones and daylight saving hours: ZoneId, ZoneOffset, and ZonedDateTime.
UTC (Coordinated Universal Time) is the primary time standard used for keeping time around the world. UTC/Greenwich is the time at Royal Observatory, Greenwich, England. It is the basis for defining time in different regions of the world.
Time Zones and Zone Offsets
A time zone defines a region that observes the same standard time. The time observed in a region is usually referred to as the local time. A time zone is described by a zone offset from UTC/Greenwich and any rules for applying daylight saving time (DST). Time zones that practice DST obviously have variable offsets during the year to account for DST.
In Java, each time zone has a zone ID that is represented by the class java.time.ZoneId. The class java.time.ZoneOffset, which extends the ZoneId class, represents a zone offset from UTC/Greenwich. For example, the time zone with US/Eastern as the zone ID has the offset -04:00 during daylight saving hours—that is, it is 4 hours behind UTC/Greenwich when DST is in effect.
The time zone offset at UTC/Greenwich is represented by ZoneOffset.UTC and, by convention, is designated by the letter Z. GMT (Greenwich Mean Time) has zero offset from UTC/Greenwich (UTC+0), thus the two are often used as synonyms; for example, GMT-4 is equivalent to UTC-4. However, GMT is a time zone, whereas UTC is a time standard.
Java uses the IANA Time Zone Database (TZDB) maintained by the Internet Assigned Numbers Authority (IANA) that updates the database regularly, in particular, regarding changes to the rules for DST practiced by a time zone (p. 1073).
A set with names of available time zones can readily be obtained by calling the ZoneId.getAvailableZoneIds() method. Time zones have unique names of the form Area/Location—for example, US/Eastern, Europe/Oslo. The following code prints a very long list of time zones that are available to a Java application.
ZoneId.getAvailableZoneIds() // Prints a long list of zone names.
.stream()
.sorted()
.forEachOrdered(System.out::println); // Output not shown intentionally.
The ZoneId.of() method creates an appropriate zone ID depending on the format of its argument:
- UTC-equivalent ID, if only “Z”, “UTC”, or “GMT” is specified. As these designations are equivalent, the result is ZoneOffset.UTC; that is, it represents the offset UTC+0.
- Offset-based ID, if the format is “+hh:mm” or “-hh:mm”—for example, “+04:00”, “-11:30”. The result is an instance of the ZoneOffset class with the parsed offset.
- Prefix offset-based ID, if the format is the prefix UTC or GMT followed by a numerical offset—for example, “UTC+04:00”, “GMT-04:00”. The result is a time zone represented by a ZoneId with the specified prefix and a parsed offset.
- Region-based ID, if the format is “Area/Location”—for example, “US/Eastern”, “Europe/Oslo”. The result is a ZoneId that can be used, among other things, to look up the underlying zone rules associated with the zone ID. In the examples in this section, a zone ID is specified in the format of a region-based ID.
The code below creates a region-based zone ID. The method ZoneId.systemDefault() returns the system default zone ID.
ZoneId estZID = ZoneId.of(“US/Eastern”); // Create a time zone ID.
System.out.println(estZID); // US/Eastern
System.out.println(ZoneId.systemDefault()); // Europe/Oslo
Selected methods in the ZoneId abstract class are presented below. The concrete class ZoneOffset extends the ZoneId class.
static ZoneId of(String zoneId)
Returns an appropriate zone ID depending on the format of the zone ID string. See the previous discussion on zone ID.
String toString()
abstract String getId()
Return a string with the zone ID, typically in one of the formats accepted by the of() method.
abstract ZoneRules getRules()
Retrieves the associated time zone rules for this zone ID. The rules determine the functionality associated with a time zone, such as daylight savings (p. 1082).
static Set<String> getAvailableZoneIds()
Returns a set with the available time zone IDs.
static ZoneId systemDefault()
Returns the zone ID of the default time zone of the system.