简体   繁体   中英

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

您还需要禁用反序列化的纳秒来使Jackson解析毫秒:

mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM