ZonedDateTime 使用 FasterXML Jackson 序列化纪元毫秒
ZonedDateTime serialize epoch milliseconds with FasterXML Jackson
我打算使用 epoch 毫秒 进行反序列化和序列化。
然而,只有反序列化有效,但未能序列化回到正确的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]
如何序列化得到2017-05-02T13:46:36.728+08:00[Asia/Singapore]
的日期?
版本com.fasterxml.jackson.*都是2.8.8
您还需要禁用纳秒反序列化以使 Jackson 解析毫秒:
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
我打算使用 epoch 毫秒 进行反序列化和序列化。
然而,只有反序列化有效,但未能序列化回到正确的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]
如何序列化得到2017-05-02T13:46:36.728+08:00[Asia/Singapore]
的日期?
版本com.fasterxml.jackson.*都是2.8.8
您还需要禁用纳秒反序列化以使 Jackson 解析毫秒:
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);