Georgi Michev
Georgi Michev

Reputation: 894

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

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\

Upvotes: 17

Views: 132746

Answers (5)

Mukti Barakoti
Mukti Barakoti

Reputation: 11

I was stuck for several hours tried every answers I found over the internet. I had to delete '.SSSZ' or '.SSS' part after '...HH:mm:ss".

Didn't work @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ", shape = JsonFormat.Shape.STRING) OR @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss[.SSS][.SS][.S]", shape = JsonFormat.Shape.STRING)

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

And setting headers in Postman Content-Type: application/json Accept: application/json

Upvotes: 1

Mike Packer
Mike Packer

Reputation: 83

I was getting this because my JSONproperty setter was not properly handling null.

e.g. 

    @Column(name = "equipment_purchase_date")
    private LocalDate purchaseDate;

bad:
    @JsonProperty
    public void setPurchaseDate(String dateStr) throws ParseException 
    {
        this.purchaseDate = LocalDate.parse(dateStr, df);
    }

good(Fix):
    @JsonProperty
    public void setPurchaseDate(String dateStr) throws ParseException 
    {
        this.purchaseDate = dateStr == null ? null : LocalDate.parse(dateStr, df);
    }

thought i would include this as none of the above answers were applicable in this case.

Upvotes: 0

Inanc Cakil
Inanc Cakil

Reputation: 336

I had the same error, I used this one with "pickupDate":"2014-01-01T00:00:00"

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
private LocalDateTime pickupDate;

Upvotes: 3

Amit Kushwaha
Amit Kushwaha

Reputation: 131

You can remove the annotation @JsonFormat and let it works in a default way. It is working fine for me even if I removed the millisecond.

@NotNull
@FutureOrPresent(message = ErrorMessages.INVALID_CAMPAIGN_START_DATE)
//@JsonFormat(pattern = "MM/dd/yyyy")
private LocalDateTime campaignStartDate;

JSON Request:

{  "campaignStartDate" : "2020-12-31T15:53:16",
  "campaignExpDate" : "2021-01-24T15:53:16",
}

{
  "campaignStartDate" : "2020-12-31T15:53:16.45",
  "campaignExpDate" : "2021-01-24T15:53:16.45",
}

{
  "campaignStartDate" : "2020-12-31T15:53:16.445",
  "campaignExpDate" : "2021-01-24T15:53:16.445",
}

These JSON requests will work fine.

Upvotes: 10

Nowhere Man
Nowhere Man

Reputation: 19545

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;

Upvotes: 27

Related Questions