ZonedDateTime arrival = departure.withZoneSameInstant(arrivingZone) ZoneId arrivingZone = ZoneId.of("Asia/Tokyo") Flight is 10 hours and 50 minutes, or 650 minutes
("LEAVING: %s (%s)%n", out1, leavingZone) ZonedDateTime departure = ZonedDateTime.of(leaving, leavingZone) ZoneId leavingZone = ZoneId.of("America/Los_Angeles") LocalDateTime leaving = LocalDateTime.of(2013, Month.JULY, 20, 19, 30)
Leaving from San Francisco on July 20, 2013, at 7:30 p.m. The ZoneRules.isDaylightSavings method determines whether it is daylight saving time when the flight arrives in Tokyo.Ī DateTimeFormatter object is used to format the ZonedDateTime instances for printing:ĭateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a") The withZoneSameInstant and plusMinutes methods are used to create an instance of ZonedDateTime that represents the projected arrival time in Tokyo, after the 650 minute flight.
It is used to represent a full date (year, month, day) and time (hour, minute, second, nanosecond) with a time zone (region/city, such as Europe/Paris).įlight example, defines the departure time for a flight from San Francisco to Tokyo as a ZonedDateTime in the America/Los Angeles time zone. ZonedDateTime class, in effect, combines the The ZonedDateTime class accommodates this scenario, whereas the OffsetDateTime and OffsetTime classes, which do not have access to the ZoneRules, do not. For example, most time zones experience a gap (typically of 1 hour) when moving the clock forward to daylight saving time, and a time overlap when moving the clock back to standard time and the last hour before the transition is repeated. ZoneRules, part of the package, to determine how an offset varies for a particular time zone. Also, XML and other network formats define date-time transfer as OffsetDateTime or OffsetTime.Īlthough all three classes maintain an offset from Greenwich/UTC time, only ZonedDateTime uses the
When would you use OffsetDateTime instead of ZonedDateTime? If you are writing complex software that models its own rules for date and time calculations based on geographic locations, or if you are storing time-stamps in a database that track only absolute offsets from Greenwich/UTC time, then you might want to use OffsetDateTime.