Java Reference
In-Depth Information
2012-05-11T07:30-05:00[America/Chicago]
Not all combinations of a LocalDateTime and a ZoneId result in a valid ZonedDateTime . There may be a gap or
overlap on the local timeline in a time zone because of the Daylight Saving Time change. For example, in the
America/Chicago time zone on March 10, 2013 at 02:00, the clock was moved an hour forward, thus leaving a gap of
one hour on the local timeline; the time between 02:00 and 02:59 did not exist. In the same America/Chicago time
zone on November 3, 2013 at 02:00, the clock was moved an hour backward, thus creating an overlap of one hour
on the local timeline; the time between 01:00 and 01:59 existed twice. The Date-Time API has well-defined rules to
handle such gaps and overlaps.
If the local datetime falls in the middle of the gap, the time is moved forward by the same
amount as the gap. For example, if you want to construct a zoned datetime for the time zone
America/Chicago for March 10, 2013 at 02:30:00, you will get March 10, 2013 at 3:30:00. The
time is moved forward by an hour, which is equal to the gap of an hour.
If the local datetime falls in the middle of the overlap, the time is valid. In the gap, two
zone offsets exist: one the earlier offset that existed before moving the clock backward and
one the later offset that exists after moving the clock backward. By default, for the time
in the gap, the zone offset that existed earlier is used. The ZonedDateTime class contains
withEarlierOffsetAtOverlap() and withLaterOffsetAtOverlap() , which let you select the
desired zone offset if the time falls in the overlap.
The following snippet of code demonstrates the results of ZonedDateTime with the time falling in the gap
and overlap:
ZoneId usChicago = ZoneId.of("America/Chicago");
// 2013-03-10T02:30 did not exist in America/Chicago time zone
LocalDateTime ldt = LocalDateTime.of(2013, Month.MARCH, 10, 2, 30);
ZonedDateTime zdt = ZonedDateTime.of(ldt, usChicago);
System.out.println(zdt);
// 2013-10-03T01:30 existed twice in America/Chicago time zone
LocalDateTime ldt2 = LocalDateTime.of(2013, Month.NOVEMBER, 3, 1, 30);
ZonedDateTime zdt2 = ZonedDateTime.of(ldt2, usChicago);
System.out.println(zdt2);
// Try using the two rules for overlaps: one will use the earlier
// offset -05:00 (the default) and another the later offset -06:00
System.out.println(zdt2.withEarlierOffsetAtOverlap());
System.out.println(zdt2.withLaterOffsetAtOverlap());
2013-03-10T03:30-05:00[America/Chicago]
2013-11-03T01:30-05:00[America/Chicago]
2013-11-03T01:30-05:00[America/Chicago]
2013-11-03T01:30-06:00[America/Chicago]
Search WWH ::




Custom Search