Unexpected behavior with setTimeZone and ISO8601 parsing
See original GitHub issueI 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:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
I noticed this issue while using
StdDateFormat
, the cause is thatDATE_FORMAT_STR_ISO8601_Z
uses"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 cloningDATE_FORMAT_ISO8601_Z
.Adding suggested test shows that this has been fixed at some point:
2.9
and2.10
branches pass at least. Will add unit test in 2.10, close this issue.