I got confused in time during java. I have worked for so long under the assumption that if the timestamp is specified as zulu time, java will take care of the offset relative to local time.
To illustrate. I am currently in BST, which has a UTC +1 offset. With that in mind, I would expect this Zulu time:
2016-09-12T13:15:17.309Z
be
2016-09-12T14:15:17.309
LocalDateTime after parsing it. This is because my system time is set to BST by default, and the above timestamp (zulu time) indicates that this is UTC time.
Instead, consider this sample:
String ts = "2016-09-12T13:15:17.309Z"; LocalDateTime parse = LocalDateTime.parse(ts, DateTimeFormatter.ISO_DATE_TIME); System.out.println(parse);
This will print:
2016-09-12T13:15:17.309
Thus, the timestamp parsed as LocalDateTime is not recognized as UTC and instead is treated as local time directly. So I thought, maybe I need to parse it as ZonedDateTime and convert it to LocalDateTime specifically in order to get the correct local time. With this test:
String ts = "2016-09-12T13:15:17.309Z"; ZonedDateTime parse = ZonedDateTime.parse(ts, DateTimeFormatter.ISO_DATE_TIME); System.out.println(parse); System.out.println(parse.toLocalDateTime());
I get the outputs:
2016-09-12T13:15:17.309Z 2016-09-12T13:15:17.309
The same conclusion for both dates.
The only way to correctly parse this that I could find is:
String ts = "2016-09-12T13:15:17.309Z"; Instant instant = Instant.parse(ts);
Fingerprints:
2016-09-12T13:15:17.309Z 2016-09-12T14:15:17.309
It is right.
So the question (s):
- Should Java time not recognize the UTC timestamp and analyze it for the correct system default setting?
- How can I use the
LocalDateTime#parse approach to get the correct result? - Should I use
Instant for everyone now and give up parsing?
The problem is that jersey/jackson java time modules parse timestamps using the ISO format and the usual LocalDateTime#parse methods. I realized that my times do not pass, since they are considered as LocalTime , while in reality they are in Zulu time.