1

When running the following code in Java 8, the Instant produced from parsing the String "2021-03-26T23:43:03+01:00[Europe/London]" is "2021-03-26T23:43:03Z"

(which afaik is right since in 2021 DST kicked in on March the 28th, so on the 26th London time and UCT should still match)

However, when running the same code in Java >=9, the resulting date is off by one hour: "2021-03-26T22:43:03Z"

Is this a bug, or is there I'm missing on here? I'm relatively new to Java.

  public void giveMeDate(){
    ZonedDateTime dateTime = ZonedDateTime.parse("2021-03-26T23:43:03+01:00[Europe/London]", DateTimeFormatter.ISO_DATE_TIME);
    System.out.println(dateTime.toInstant());
  }
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
MTdP
  • 11
  • 3
  • See [Java Platform, Standard Edition Troubleshooting Guide, 8 Time Zone Settings in the JRE](https://docs.oracle.com/javase/9/troubleshoot/time-zone-settings-jre.htm#JSTGD362): there is a chapter on how to find out which version of the time zone DB is used by your runtime. – Hulk Aug 11 '21 at 08:11
  • The string you try to parse is incorrect. It says `+01:00[Europe/London]` but `[Europe/London]` has an offset of `UTC+0`. `UTC+1` only kicks in if **DST** is active. Which, as you said, was not the case for this date. Since you specified `+01:00` nonetheless, Java is kinda forced to take your string for granted and subtract the hour to achieve UTC. In old versions (before Java 9), there was a bug (linked above) that caused it to fail or ignore this edge case. Starting from Java 9, it correctly does what you tell it to do, resulting in the hour being corrected back again. – Zabuzard Aug 11 '21 at 08:18
  • Tldr, if you correct your string to `2021-03-26T23:43:03+00:00[Europe/London]` (`+00:00`), it will also stay at `23:43:03` after the conversion, as desired. – Zabuzard Aug 11 '21 at 08:20
  • You can also see this if you are going the reverse: `Instant instant = Instant.parse("2021-03-26T23:43:03Z");` and then `ZonedDateTime dateTime = instant.atZone(ZoneId.of("Europe/London"));`. Printing them gives the same time. But if you increase to April, you will get an offset due to DST. – Zabuzard Aug 11 '21 at 08:24

0 Answers0