Java - Two zone offsets available for the local time in the specified zone

Introduction

In case there are two zone offsets available for the local time in the specified zone.

If the specified referred zone offset is invalid, the earlier zone offset for the overlap is used.

When I provide an invalid preferred offset -07:00, the earlier offset -05:00 is used.

Demo

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

public class Main {
  public static void main(String[] args) {
    ZoneId usChicago = ZoneId.of("America/Chicago");
    ZoneOffset offset5 = ZoneOffset.of("-05:00");
    ZoneOffset offset6 = ZoneOffset.of("-06:00");
    ZoneOffset offset7 = ZoneOffset.of("-07:00");

    // At 2013-10-03T01:30, -05:00 and -06:00 offsets were valid for
    // the time zone America/Chicago
    LocalDateTime ldt = LocalDateTime.of(2013, Month.NOVEMBER, 3, 1, 30);
    ZonedDateTime zdt5 = ZonedDateTime.ofLocal(ldt, usChicago, offset5);
    ZonedDateTime zdt6 = ZonedDateTime.ofLocal(ldt, usChicago, offset6);
    ZonedDateTime zdt7 = ZonedDateTime.ofLocal(ldt, usChicago, offset7);
    System.out.println("With offset " + offset5 + ": " + zdt5);
    System.out.println("With offset " + offset6 + ": " + zdt6);
    System.out.println("With offset " + offset7 + ": " + zdt7);
  }/*w ww.j  av a  2  s . c o m*/
}

Result

Related Topic