I am having trouble converting a java.util.Date
into an java.time.LocalDateTime
and I am having a really weird effect with regard to the timezone:
Date date = new Date(-3155677200000L); // 1870-01-01T00:00:00.000+0100
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); // 1869-12-31T23:53:28
The time is really off. 6 minutes and 32 seconds, which is annoying, because it also changes the date and even year here.
It has to do with ZoneId.systemDefault()
, if I use a ZoneOffset
it works.
Why is that? Is it a bug in the JDK? In only seems to happen for dates before year 1893.
ZoneId.systemDefault()
is Europe/Berlin (CET, +01:00)
Similarly:
ZonedDateTime zonedDateTime = LocalDateTime.of(1870, 1, 1, 0, 0, 0, 0).atZone(ZoneId.systemDefault());
prints:
1870-01-01T00:00+00:53:28[Europe/Berlin]
expected result:
1870-01-01T00:00+01:00:00[Europe/Berlin]
The conversion is correct according to the IANA rules. The Europe/Berlin rule starts with this line:
Zone Europe/Berlin 0:53:28 - LMT 1893 Apr
You started off with 1869-12-31T23:00:00Z, so the local time is 1869-12-31T23:53:28.
No bugs here - just a mistaken expectation.
See more on this question at Stackoverflow