Java - Results of ZonedDateTime with the time falling in the gap and overlap:

Description

Results of ZonedDateTime with the time falling in the gap and overlap:

Demo

import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
  public static void main(String[] args) {
    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);//w w w .  j av a  2s  . c o m

    // 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());
  }
}

Result

Related Topic