Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZonedDateTime serialize epoch milliseconds with FasterXML Jackson

I'm intended to use epoch millisecond for the deserialization and serialization. However only the deserialzation works but failed to serialize back to the correct ZonedDateTime.

ObjectMapper mapper = new ObjectMapper();
mapper.setTimeZone(TimeZone.getDefault());
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);

System.out.println(mapper.writeValueAsString(ZonedDateTime.now()));  // print 1493703996728 [Expected]
System.out.println(mapper.readValue("1493703996728", ZonedDateTime.class)); // print +49303-08-07T00:52:08+08:00[Asia/Singapore] [Unexpected]

how to make the serialize to get the date of 2017-05-02T13:46:36.728+08:00[Asia/Singapore]?

version of com.fasterxml.jackson.* are all 2.8.8

like image 430
ChinKang Avatar asked May 02 '17 05:05

ChinKang


1 Answers

You also need to disable nanoseconds for deserialization to make Jackson parse milliseconds:

mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
like image 109
Manos Nikolaidis Avatar answered Sep 19 '22 16:09

Manos Nikolaidis