1

Having an issue with the FasterXML ObjectMapper setting the duedate incorrectly when serializing. updated and created are fine when mapping. Only difference is the hours are not included and are not provided.

"fields": {
    "updated": "2020-09-01T06:18:36.000+0900",
    "duedate": "2020-08-04",
    "created": "2020-07-31T08:25:54.000+0900"
}

This is my code:

ObjectMapper mapper = new ObjectMapper();
Fields fields = mapper.readValue(json, Fields.class);
System.out.println(fields.getFields().getDuedate());
Result: Mon Aug 03 19:00:00 CDT 2020

As you can see its off by a day and dont know where the hours came from. The result should be: Tue Aug 04 00:00:00 CDT 2020

Tags
  • 172
  • 3
  • 16
  • From the output, you can see it parsed the date in CDT timezone (maybe the timezone for your server?) Looking purely at the JSON it's completely ambiguous when that due date is supposed to be. I assume +0900 because of the other times. If that's the case, you can write a custom deserialize method to copy the timezone across from those fields. – Hitobat Sep 04 '20 at 21:10
  • Take a look on similar question: [POSTMAN is returning date fields with changed values](https://stackoverflow.com/questions/63624760/postman-is-returning-date-fields-with-changed-values/63625504#63625504). `java.util.Date` `toString` method shows date in default timezone. – Michał Ziober Sep 05 '20 at 16:51

1 Answers1

1

Java is taken your local time zone by default, and convert all the dates in your local time zone, in your case if your are using jdk8 and up, and you need to keep the date your need to use:

@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate duedate;

with this change your ar going to keed the date without transformation, and you can use OffsetDateTime or ZonedDateTime in the other cases to keep the original date.

I hope LocalDate works for you.

Marco
  • 517
  • 3
  • 10