Reputation: 103
I have a problem with the dates and their passage from Date to LocalDate, mainly the error is with the dates that have the year 1700. I have tried two different dates and whenever the year is 1700, it puts me one day less.
Date dto = ...;
Instant inst = dto.toInstant();
LocalDate localDate = LocalDate.ofInstant(inst, ZoneId.systemDefault());
Date dto It is a variable that retrieves the date of a query. But the method only gets dto variable as input parameter.
Tue Dec 14 00:00:00 CET 1700 -> Date
1700-12-13T23:00:00Z -> Instant
1700-12-13 -> LocalDate
Sat Jan 01 00:00:00 CET 1994 -> Date
1993-12-31T23:00:00Z -> Instant
1994-01-01 -> LocalDate
I use:
jackson.time-zone: ${TZ:Europe/Madrid}
What is the problem with this year?
Upvotes: 3
Views: 413
Reputation: 272370
This is because whoever produced that Date
and ZoneId.systemDefault()
have different opinions on what Madrid's offset is from UTC, back in 1700.
Whoever produced that Date
mistakenly thinks that Madrid is UTC+1 back in 1700-12-14, so it produced a Date
that represents the midnight of 1700-12-14 in the zone UTC+1 ("CET"):
Tue Dec 14 00:00:00 CET 1700
This has a corresponding Instant
, because we can pinpoint a certain point in time using this information. This is what toInstant
gives you. Instant.toString
always displays in UTC (more specifically the ISO 8601 format), so you see:
1700-12-13T23:00:00Z
1700-12-14T00:00:00+01:00 is indeed the same point in time as 1700-12-13T23:00:00Z.
When you do LocalDate.ofInstant
, you use ZoneId.systemDefault()
. ZoneId.systemDefault()
thinks that Madrid had the offset of UTC-0:14:44. This is because Madrid had been observing Local Mean Time, and did not standardise timezones until the 1900s.
Therefore, LocalDate.ofInstant
subtracts 14 minutes and 44 seconds from 1700-12-13T23:00:00Z to compute what the date would be at offset -0:14:44, and lo and behold, it is 1700-12-13.
I would recommend that you not to use Date
if possible. If what you are doing is related to time at all, you should work with LocalDate
s directly.
Upvotes: 3