3

I'm using jackson-datatype-jsr310 to support Java 8 time data types.

I created a ObjectMapper like this:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
objectMapper.registerModule(new JavaTimeModule());

Foo foo = new Foo();
LocalDateTime t = LocalDateTime.now();
foo.setT1(Date.from(t.toInstant(ZoneOffset.UTC)));
foo.setT2(Timestamp.valueOf(t));
foo.setT3(t);

System.out.println(objectMapper.writeValueAsString(foo));

And parsed a LocalDateTime then I got [2016,12,21,15,53,57,178] not 1482306837178

Here is my test model class Foo

public class Foo {

    private Date t1;

    private Timestamp t2;

    private LocalDateTime t3;

    // Getters & Setters
}

I see the question Jackson Java 8 DateTime serialisation and the problem owner get a serialized string like 1421261297.356000000. I did't known how to get that. I'm using jackson 2.8.5 same with datatype-jsr310.

Community
  • 1
  • 1
bitdancer
  • 1,215
  • 2
  • 19
  • 34

1 Answers1

5

From the Jackson Modules Datetime documentation:

LocalDate, LocalTime, LocalDateTime, and OffsetTime, which cannot portably be converted to timestamps and are instead represented as arrays when WRITE_DATES_AS_TIMESTAMPS is enabled.

Full doc page here

kunruh
  • 874
  • 1
  • 8
  • 17