Skip to content
Advertisement

Why Jackson ObjectMapper give me this error converting JSON field into a Date object? Can not deserialize value of type java.util.Date from String

I have this JSON object:

JavaScript

that have to be mappend on this DTO class:

JavaScript

To do this mapping I am using Jackson ObjectMapper, in this way:

JavaScript

When I try to map the JSON dateTime field with the Date dateTime in my DTO class I am obtaining the following exception message:

JavaScript

As you can see the value of the dateTime field into my JSON document is 2020-06-13 20:00:15 (it have to contains the date and the time). I suspect that this is not in the correct format.

How can I solve the issue? Have I to change the format into my JSON object or have I to set some Jackson configuration?

Advertisement

Answer

Use @JsonFormat to deserialize for your format.

JavaScript

You can specify timezone also like timezone = "IST" in the annotation or as default timezone like timezone = JsonFormat.DEFAULT_TIMEZONE

As your JSON string format is different than the default format you have to specify the format.

I recommend you don’t use Date. This class is poorly designed and long outdated. Use LocalDateTime from java.time API

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement