LocalDateDeserializer ignores local time zone
See original GitHub issueI expect the LocalDateDeserializer to return a date in the local time zone.
Following snippet can reproduce the wrong behaviour.
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
LocalDate date = mapper.readValue("\"2017-12-31T23:00:00.000Z\"", LocalDate.class);
System.out.println(date); // Output: 2017-12-31
IMHO should this code executed in GMT+1 print “2018-01-01”. This could be easily fixed. Replace this line:
// com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer.deserialize(JsonParser, DeserializationContext)
...
// replace this
return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC).toLocalDate();
// with this
return ZonedDateTime.parse(string).withZoneSameInstant(ZoneId.systemDefault()).toLocalDate();
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Java 8 LocalDate Jackson format - json - Stack Overflow
RELEASE,I just registered java time module with no explicit annotation in POJO class having LocalDate field & it worked. var objectMapper = new ......
Read more >Jackson Date - Baeldung
In this tutorial, we'll serialize dates with Jackson. We'll start by serializing a simple java.util.Date, then Joda-Time, and finally, ...
Read more >Formatting Java Time with Spring Boot using JSON - TouK
I would like to return class Clock , containing LocalDate , LocalTime and LocalDateTime , preinitialized in constructor.
Read more >JavaTimeModule.addDeserializer - Tabnine
How to use. addDeserializer. method. in. com.fasterxml.jackson.datatype.jsr310.JavaTimeModule · Best Java code snippets using com.fasterxml.jackson.datatype.
Read more >OffsetDateTime (Java Platform SE 8 ) - Oracle Help Center
OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be obtained. ZonedDateTime adds full time-zone rules. It ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@cowtowncoder The local timezone may not be important, but take this case: Web application, a particular date is saved as LocalDate because it actually does not care about time zone. The user being in GMT+2 zone, picks Apr 30th 2020, so he’ll expect to see Apr 30th 2020 back, however, he sees Apr 29th 2020. How: The user chose Apr 30th 2020, he is in GMT+2. Since it is a date object, javascript sends it as “2020-04-29T22:00:00.000Z” In backend they expect a LocalDate so Jackson translates that string. Since it does not care about the locale of the user, the date becomes Apr 29th 2020. So while the server’s locale should not be used, the locale of the request should. And btw, this is a real case in the application I work in right now
@kupci I’m afraid not - the scenario described by dariotortola is not handled by #94 - I just updated jackson to newest version in the hope it would be, but the fix of #94 is:
So this basically means: cut off the Z and parse it as “2020-04-29T22:00:00.000”.
Also if this string should be parsed as LocalDate (without Time) the code goes like this with the same result:
return string.endsWith("Z") ? LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC).toLocalDate() : ...;
So long story short: I think you must tell your client somehow not to convert the “2020-04-30” into a “2020-04-29T22:00:00.000Z” when sending it to the server.
If I am mistaken somewhere and there actually is a way to configure jackson to make a “2020-04-29T22:00:00Z” into “200-04-30”-LocalDate, please tell me! It would be very helpful!