0

I am trying to parse dates from strings to ZonedDateTimes and I've come across a bizzare problem.

2020-11-01T01:00-05:00[America/New_York]

This is an hour right after time EDT ends this year. When I pass it to ZonedDateTime.parse I get

ZonedDateTime.parse("2020-11-01T01:00-05:00[America/New_York]")
// 2020-11-01T01:00-04:00[America/New_York]

but if I do

ZonedDateTime.parse("2020-11-01T01:00-04:00[America/New_York]").plusHours(1)

I get

2020-11-01T01:00-05:00[America/New_York]

So it's not like Java cannot represent this ambiguous value or something..

Can anyone explain to me that behavior and possible solution?

Note: I am using Java 8

bottaio
  • 4,963
  • 3
  • 19
  • 43

1 Answers1

2

As Amir Schnell said in the comments, this seems to be a bug in the JDK, as they cannot reproduce this in Java 11.

For now, I have found this work around:

Parse the string into a local date time, zone ID, and zone offset, and create a ZonedDateTime using those three things:

TemporalAccessor ta = DateTimeFormatter.ISO_ZONED_DATE_TIME.parse("2020-11-01T01:00-05:00[America/New_York]");
System.out.println(
    ZonedDateTime.ofLocal(
        LocalDateTime.from(ta), 
        ZoneId.from(ta), 
        ZoneOffset.from(ta)
    )
);
Sweeper
  • 213,210
  • 22
  • 193
  • 313