6

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

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
ChinKang
  • 4,212
  • 2
  • 17
  • 29
  • 1
    It seems you desrialization understands 1493703996728 as seconds since the epoch, not milliseoncds. Don’t know how that can be. – Ole V.V. May 02 '17 at 09:04
  • yes indeed, it will parse correctly if it is `1493703996.728`, but this is not what i want :( – ChinKang May 02 '17 at 09:09

1 Answers1

7

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

mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
  • Thank you so much Manos! i'm keep looking at the options inside `SerializationFeature` enum, but totally overlooked of `DeserializationFeature` – ChinKang May 02 '17 at 10:11