简体   繁体   English

ZonedDateTime使用FasterXML Jackson序列化纪元毫秒

[英]ZonedDateTime serialize epoch milliseconds with FasterXML Jackson

I'm intended to use epoch millisecond for the deserialization and serialization. 我打算使用epoch毫秒来进行反序列化和序列化。 However only the deserialzation works but failed to serialize back to the correct ZonedDateTime . 但是,只有反序列化工作,但无法序列化回正确的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] ? 如何让序列化获取2017-05-02T13:46:36.728+08:00[Asia/Singapore]

version of com.fasterxml.jackson.* are all 2.8.8 com.fasterxml.jackson。*的版本都是2.8.8

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

mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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