0

I'm using a rest template to call an API in Java, which returns this object :

@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
public class MyObject {
    //@JsonDeserialize(using = LocalDateTimeDeserializer.class)
    private LocalDateTime accessTime;
    private String name;
}

The problem is that the accessTime retrieved by the API looks like this :

2020-04-29 12:52:57

And I can't use a LocalDateTimeDeserializer.

This is the following error:

Can not deserialize value of type java.time.LocalDateTime from String \"2020-04-29 12:52:57\": Text '2020-04-29 12:52:57' could not be parsed at index 10

How can I handle this ? Thank you

  • Do [this question and its accepted answer](https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper) help? – deHaar Apr 30 '20 at 15:11
  • Assuming you're using the `JavaTimeModule`, Jackson is already using `LocalDateTimeDeserializer`, so it would be redundant to annotated your field. – Sotirios Delimanolis Apr 30 '20 at 15:13
  • @deHaar I saw this answer before posting my question, but I have the following error "Attribute value must be constant" at "@DateTimeFormat(iso = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))" – JeremCxnone Apr 30 '20 at 15:25

1 Answers1

0

I guess you are looking for a method which will receive the local date time in string from the API and then use this method and store the converted localdatetime format. This is the method which can help you:

private LocalDateTime TryPase(string accessTime)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); //edit your format
return LocalDateTime.parse(accessTime, formatter);
}
Rithik Banerjee
  • 447
  • 4
  • 16