Java – Jackson to deserialize Joda LocalDate from ISO DateTime

date-parsingdatetimejacksonjavajodatime

I am using Angular date picker to send my MVC controller a date, using a Javascript Date object which is an ISO date/time.

On deserializing java.util.Date it works like a charm and Hibernate will care about cutting that Datetime to a plain Date when inserting records.

But now that I am transitioning from java.util.Date to org.joda.time.[APPROPRIATE_CLASS_HERE] I am facing this deserialization issue.

It is my understanding that if I force DateTime in my DTOs Jackson will deserialize them correctly, while instead I prefer to drop time information when the target type is a Date.

E.g.

public class UserDto {

    private LocaLDate passwordExpirationDate;

}


{
   "username":"9493",
   "completeName":"ljdjf",
   "email":"wesf@dsgfds",
   "cultureId":"IT",
   "enabled":false,
   "passwordExpirationDate":"2017-07-13T10:00:00.000Z",
   "accountExpirationDate":"2017-07-20T10:00:00.000Z"
}

Instead I get this:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z"; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z" (through reference chain: it.phoenix.web.data.dtos.admin.profile.UserDTO["passwordExpirationDate"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:244) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z" (through reference chain: it.phoenix.web.data.dtos.admin.profile.UserDTO["passwordExpirationDate"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388) ~[jackson-databind-2.8.9.jar:2.8.9]
Caused by: java.lang.IllegalArgumentException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z"
    at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:900) ~[joda-time-2.9.9.jar:2.9.9]
    at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:844) ~[joda-time-2.9.9.jar:2.9.9]
    at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:39) ~[jackson-datatype-joda-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:15) ~[jackson-datatype-joda-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504) ~[jackson-databind-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104) ~[jackson-databind-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:357) ~[jackson-databind-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148) ~[jackson-databind-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814) ~[jackson-databind-2.8.9.jar:2.8.9]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2938) ~[jackson-databind-2.8.9.jar:2.8.9]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 92 more

Question is: is there a smart way so that Jackson can decode DateTime object into a Joda LocalDate by simply stripping the time part at default/current time zone?

Notes:
– I already have Jackson Joda module dependency
– Jackson is 2.8.9
– I am forced to use Java 7. In a related Java 8 project I have no such problem with java.time stuff (and Jackson JSR310 module)

Best Answer

According to the error message, the date/time input is coming as 2017-07-13T10:00:00.000Z and LocalDate by default can't handle it.

You can config this format by using the com.fasterxml.jackson.annotation.JsonFormat annotation in the LocalDate field:

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
private LocalDate passwordExpirationDate;

This will make Jackson parse the date correctly.