JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String

Tags: , , , ,



I am sending request to external service which has updatedDate property

@UpdateTimestamp
@Column(name = "updated_date")
private LocalDateTime updatedDate;

When I receive the response in my DTO I am trying to format the LocalDateTime property like this

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime updatedDate;

But I get error in Postman

"message": "JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-04-14T10:45:07.719": Text '2020-04-14T10:45:07.719' could not be parsed at index 14; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-04-14T10:45:07.719

Answer

There are milliseconds in the input string, so your format should be “yyyy-MM-dd’T’HH:mm:ss.SSS”

Update: If the millisecond part consists of 1, 2, 3 digits or is optional, you may use the following format:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS][.SS][.S]")
private LocalDateTime updatedTime;


Source: stackoverflow