question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LocalDateDeserializer ignores local time zone

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dariotortolacommented, Apr 21, 2020

@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

0reactions
fsomme2scommented, Mar 9, 2021

@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:

if (string.endsWith("Z")) {
     if (isLenient()) {
         return LocalDateTime.parse(string.substring(0, string.length()-1),
                 _formatter);
     }

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found