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.

Behaviour of custom date format with regards to the ObjectMapper's default time zone

See original GitHub issue

Tested with Jackson 2.8.8 and 2.8.9.

The general rule with the StdDateFormat with regards to the TimeZone is:

     the TimeZone of the ObjectMapper is used if the JSON input does not contain
     any timezone/offset information.

     If the mapper timezone is set to GMT+1, the input 2000-01-01T00:00:00.000
     (no timezone) will produce a date 2000-01-01T00:00:00.000+01:00

This rule remains valid when the @JsonFormat annotation is used on a property unless it forces an explicit timezeone, in which case it takes precedence.

One would expect the same behavior when the StdDateFormat is replaced by a custom DateFormat on the ObjectMapper. In other words, the timezone of the DateFormat should be of no importance since the ObjectMapper’s default is used whenever needed.

However it appears this behaviour is not always respected and depends on whether the mapper has been explicitly configured with a TimeZone or not.

Suppose the following custom date format:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'X'HH:mm:ss");
df.setTimeZone( TimeZone.getTimeZone("GMT+4") );

Use it on a vanilla ObjectMapper without any further configuration:

ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(df);
Date d = mapper.readValue("2000-01-02X04:00:00", Date.class);

This produces a date with GMT+4 although we would expect UTC according to the “rules” described above.

Do a second test and use the same date format on an ObjectMapper whose timezone is explicitly set to UTC. Since UTC is the default, this test should behave as the first…

ObjectMapper mapper = new ObjectMapper();
mapper.setTimeZone(TimeZone.getTimeZone("UTC"));
mapper.setDateFormat(df);
Date d = mapper.readValue("2000-01-02X04:00:00", Date.class);

Now this produces a date in UTC and not GMT+4 anymore… This is compliant with the rules, but unexpected in the light of the first test. Do the test once more with GMT+2 instead go UTC and the date will be in GMT+2

This case is covered by the DateDeserializationTest#testDateUtil_customDateFormat_withoutTZ() test case submitted in PR https://github.com/FasterXML/jackson-databind/pull/1660.

Could this be related to https://github.com/FasterXML/jackson-databind/blob/2.8/src/main/java/com/fasterxml/jackson/databind/cfg/BaseSettings.java#L245-L249 ?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Martin-Luftcommented, Jun 27, 2017

I’m not sure if this is the same issue:

Until 2.9: I set the objectMapper.setTimeZone() and the string 2016-10-30T17:00:00 was interpreted with the given time zone and not as UTC.

Since 2.9: The string 2016-10-30T17:00:00 is interpreted as UTC (ignoring my set time zone) which breaks my application logic.

1reaction
cowtowncodercommented, Jun 27, 2017

@Martin-Wegner it definitely sounds like #1657, which may have made it in 2.9.0.pr4 (not 100% sure, fix is quite close to release time), but is definitely in master.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson Date - Baeldung
It's important to note that Jackson will serialize the Date to a timestamp format by default (number of milliseconds since January 1st, ...
Read more >
Default timezone for DateTime deserialization with Jackson ...
So, to answer your question about time zones, it sounds like by default Jackson always serializes using UTC (no time zone offset) which...
Read more >
Spring Jackson custom date format - Java Developer Zone
1. Default date format is Long Timestamp (Default) · 2. Format date using @JsonFormat annotation · 3. Format date and time @JsonFormat annotation ......
Read more >
How to convert Java Date into Specific TimeZone format
In this example, I am converting a Java Date object from one timezone to another. We will use SimpleDateFormat class to format the...
Read more >
Jackson JSON Request and Response Mapping in Spring Boot
We can disable this behavior to allow Jackson to convert Date and Time types of objects into human readable String format. Please note...
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