Home > Enterprise >  java.time.Instant.atZone(...) far in the past return crazy values
java.time.Instant.atZone(...) far in the past return crazy values

Time:02-15

I detect the following output when converting an Instant that is far in the past to a ZonedDateTime.

Code as Unit-Test:

   @Test
public void testParseDate() {
    Instant instant = Instant.parse("0000-12-30T07:00:00Z");
    System.out.println(instant);
    System.out.println(instant.atZone(ZoneId.of("Europe/Berlin")));

    instant = Instant.parse("1800-12-30T07:00:00Z");
    System.out.println("---------------------------------");
    System.out.println(instant);
    System.out.println(instant.atZone(ZoneId.of("Europe/Berlin")));

    instant = Instant.parse("1900-12-30T07:00:00Z");
    System.out.println("---------------------------------");
    System.out.println(instant);
    System.out.println(instant.atZone(ZoneId.of("Europe/Berlin")));
}

Output:

0000-12-30T07:00:00Z
0000-12-30T07:53:28 00:53:28[Europe/Berlin]
---------------------------------
1800-12-30T07:00:00Z
1800-12-30T07:53:28 00:53:28[Europe/Berlin]
---------------------------------
1900-12-30T07:00:00Z
1900-12-30T08:00 01:00[Europe/Berlin]

In the first two outputs the time is 07:53:28 00:53:28 and the last output is 08:00 01:00[Europe/Berlin]. Maybe the root cause is that the time zone stuff doesn't yet exists in the years of the first two examples, but the offset of 00:53:28 is really strange. Any idea where this offset " 00:53:28" comes from?

CodePudding user response:

Because history.

On April 1st, 1893, the time zone was adjusted from Local Mean Time (LMT), a local time zone (which was common at that time), to Central European Time (CET), adjusting the clock by 6 minutes and 32 seconds.

Timezones are regularly changed due to political decisions, but those strange amounts like 6 minutes and 32 seconds are often the result of moving from a local mean time to a standardized time.

Many known historical changes are found at timeanddate.com, and Berlin specifically at https://www.timeanddate.com/time/zone/germany/berlin.

CodePudding user response:

Timezones, Daylight Saving, and all those annoying and fancy features, did not exist at the beginning of the 20th century.

There were many "variations" of the time, like the infamous Dutch Time which was about 19 minutes ahead of UTC, mostly based on the clock of a tower which was simply that amount of time ahead.

I don't know exactly where the time difference comes from in the case of Germany and Berlin, but it's a known offset (likely based on similar reasons as the Dutch Time).

You can see how it has evolved over time.

  •  Tags:  
  • java
  • Related