'Why can't I deserialize value of date in Jackson?

Cannot deserialize value of type java.util.Date from String "2021-04-20T07:49:18.19Z": expected format "yyyy-MM-dd'T'HH:mm:ss.SSZ"

The pattern seems to be fine for both serialize and deserialize objects.



Solution 1:[1]

This works for me.

public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    DeserializedObject deserializedObject = objectMapper.readValue(
        "{\n" +
            "\"date\": \"2021-04-20T07:49:18.19Z\"\n" +
        "}",
        DeserializedObject.class);
}


static class DeserializedObject {

    @JsonProperty("date")
    private Date date;
}

Solution 2:[2]

the right pattern for "2021-04-20T07:49:18.19Z" is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" you can retry this

then put a JsonFormat on the field

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") private Date date;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2