0

I have the following object from the response

{
"upload_date": "2021-09-24 00:00:00"
}

I am using jackson to deserialize into LocalDateTime field

@Getter
@Builder
@JsonDeserialize(builder = AdGroup.AdGroupBuilder.class)
public class AdGroup {

    @JsonProperty("upload_date")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime uploadDate;

}

But I get 'Cannot deserialize value of type java.time.LocalDateTime from String "2021-09-24 00:00:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2021-09-24 00:00:00' could not be parsed at index 10' I tried to use "yyyy-MM-dd'T'HH:mm:ss" pattern, with/without 'shape = JsonFormat.Shape.STRING' but the same error always.

Ip Man
  • 77
  • 10
  • 1
    Does the accepted answer of https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper help? – Seelenvirtuose Dec 07 '21 at 18:09
  • Does this answer your question? [Deserialize Java 8 LocalDateTime with JacksonMapper](https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper) – João Dias Dec 07 '21 at 18:41
  • I didn't add an important thing that I also use lombok @Builder so that was the cause of the error. – Ip Man Dec 08 '21 at 16:48

2 Answers2

0

Can you try -

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'")

And value exmaple - "2021-12-08T16:49:02.449Z"

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
0

Gets fixed by:

@Getter
@Builder
@JsonDeserialize(builder = AdGroup.AdGroupBuilder.class)
public class AdGroup {
        
   public static class AdGroupBuilder {
   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
   private LocalDateTime uploadDate;
   }

@JsonProperty("upload_date")
private LocalDateTime uploadDate;

}
Ip Man
  • 77
  • 10