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.

Unexpected behavior with setTimeZone and ISO8601 parsing

See original GitHub issue

I am using objectMapper.setTimeZone(TimeZone.getTimeZone("CET")) to alter the output of timezones when using objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);. This all works fine, but it seems that specifying a timezone also causes Jackson to misinterpret the Z in incoming ISO8601 times. The following testcase describes this issue:

@Test
public void testBehavior() {
  ObjectMapper objectMapper = new ObjectMapper();

  objectMapper.setTimeZone(TimeZone.getTimeZone("CET"));

  String time = "2016-01-01T17:00:00.000Z";
  Date dateAccordingToJackson = objectMapper.convertValue(time, Date.class);
  Date correctDate = Date.from(Instant.from(DateTimeFormatter.ISO_DATE_TIME.parse(time)));

  Assert.assertEquals("Demonstrate Jackson bug(?)", correctDate.toString(),
      dateAccordingToJackson.toString());
}

With the following output:

Demonstrate Jackson bug(?) expected:<Fri Jan 01 1[8]:00:00 CET 2016> but was:<Fri Jan 01 1[7]:00:00 CET 2016>

It seems to me that this is incorrect behavior since as far as I know ISO8601 mandates that a trailing Z indicates the time is transmitted in the UTC timezone. The documentation does say it affects all timezone behavior, but I don’t think this should be happening.

I’ve tested with Jackson 2.7.2

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ecarloslrcommented, Oct 19, 2016

I noticed this issue while using StdDateFormat, the cause is that DATE_FORMAT_STR_ISO8601_Zuses "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as the format to use when the date comes with a Z. Notice that the Z is considered a literal, meaning it will be ignored completely.

When cloning the object for this pattern the CET TimeZone is used, and thus when parsing the date it will be interpreted in the CET TimeZone.

I believe the solution is to use DEFAULT_TIMEZONE (UTC) instead of _timezone when cloning DATE_FORMAT_ISO8601_Z.

0reactions
cowtowncodercommented, Apr 7, 2020

Adding suggested test shows that this has been fixed at some point: 2.9 and 2.10 branches pass at least. Will add unit test in 2.10, close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Parse ISO8601 date string to date with UTC Timezone
I am trying to serialize/deserialize a date from/to a JavaScript application. Server side, I use Java, JodaTime is installed on it. I found...
Read more >
A variation of Time.iso8601 that can parse yyyy-MM-dd HH ...
Let me propose a String to Time conversion method that can parse "yyyy-MM-dd HH:mm:ss" format, which is very much similar to Time.iso8601 ,...
Read more >
Time, TIMETZ, Timestamp and TimestampTZ in PostgreSQL
It disregards time zones when parsing, which can be unexpected. Alternatively, think of it as “always UTC time”. TIMESTAMPTZ is a type with...
Read more >
Documentation: 15: 8.5. Date/Time Types - PostgreSQL
See Appendix B for the exact parsing rules of date/time input and for the recognized ... they can have surprising behavior when the...
Read more >
Date.parse() - JavaScript - MDN Web Docs
Given a non-standard date string of "March 7, 2014" , parse() assumes a local time zone, but given a simplification of the ISO...
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