2

ZonedDateTime doesn't seem to allow to detect if the date given is in a DST gap or overlap. The docs say for the of method (creating a ZonedDateTime):

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".

In the case of a gap, when clocks jump forward, there is no valid offset. Instead, the local date-time is adjusted to be later by the length of the gap.

^ as read above, this method will just return a value but will not allow me to know if a DST gap or overlap occurs.

I would like to know if the date provided is in a gap or overlap, and have the starting date and ending date of the gap or overlap.

For example:

"1920-09-01 00:10" for time zone "Africa/Accra" is in a DST gap.
The gap is from "1920-09-01 00:00:00+00:00" to "1920-09-01 00:20:00+00:20".

"1920-12-30 23:50" for time zone "Africa/Accra" is in a DST overlap.
The overlap concerns the periods:
"1920-12-30 23:40:00+00:20" to "1920-12-31 00:00:00+00:20" and
"1920-12-30 23:40:00+00:00" to "1920-12-31 00:00:00+00:00"

How can I get these dates in case of a gap or overlap as in the examples above?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user2923322
  • 1,072
  • 1
  • 11
  • 25

1 Answers1

3

I don't think it is physically possible to create a ZonedDateTime that is in a gap transition, because the local date times that a gap transition skips over are technically "non-existent", and ZonedDateTime is quite strict about this. Depending on your method, it will either throw an exception or adjust to a time that exists.

You should do this check with LocalDateTimes, not ZonedDateTimes.

ZoneRules has this very convenient method called getTransition that takes a LocalDateTime and gives you a ZoneOffsetTransition, which represents a gap/overlap transition, or null if that date time doesn't have a transition. Example:

System.out.println(ZoneId.of("Africa/Accra").getRules()
                    .getTransition(LocalDateTime.of(1920, 9, 1, 00, 10)));

Output:

Transition[Gap at 1920-09-01T00:00Z to +00:20]

You can then get all the information you want (start, end, offset before, offset after, gap/overlap etc) from the ZoneOffsetTransition object.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 2
    A ZoneOffsetTransition actually has `isGap` and `isOverlap` methods, in fact. – VGR Feb 09 '20 at 17:15