Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java 8 time api choose the offset on DST change period

Tags:

In Java 8 time API, you can create a LocalDateTime which falls in the overlap of time during the DST time change in autumn (Central European Time).

You can convert this to a ZonedDateTime which represents a precise moment in time using a ZoneId.

Doing this does not actually resolve the ambiguity - there could still be two moments in time which correspond to this LocalDateTime and this Zone (but at different offsets).

How and why (reference welcome) does the time API choose the summer offset?

@Test
public void TimeSetOnDST() throws Exception {
    LocalDateTime time = LocalDateTime.of(2016, 10, 30, 2, 30); // in the DST time overlap
    ZonedDateTime of = ZonedDateTime.of(time, ZoneId.of("Europe/Zurich"));
    System.out.println(of); // 2016-10-30T02:30+02:00[Europe/Zurich]
    // But why not 2016-10-30T02:30+01:00[Europe/Zurich] ?
    // Is this just "by convention"?
}
like image 224
Philipp Avatar asked Oct 12 '16 08:10

Philipp


1 Answers

It's well documented in the javadoc:

In most cases, there is only one valid offset for a local date-time. In the case of an overlap, when clocks are set back, there are two valid offsets. This method uses the earlier offset typically corresponding to "summer".

As described in the comments by @JodaStephen, if you want to choose the other available offset, you can use the withLaterOffsetAtOverlap method.

like image 74
assylias Avatar answered Oct 13 '22 03:10

assylias