Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom deserializing a date with format

Tags:

java

jackson

["last_modified"])] with root cause java.time.format.DateTimeParseException: Text '2018-06-06T13:19:53+00:00' could not be parsed, unparsed text found at index 19

The inbound format is 2018-06-06T13:19:53+00:00
It's a weird format.

I have tried the following:

public class XYZ {  
    @DateTimeFormat(pattern = "yyyy-MM-ddTHH:mm:ss+00:00", iso = ISO.DATE_TIME)
    private LocalDateTime lastModified;
}  
like image 642
Jim Avatar asked Oct 18 '25 11:10

Jim


1 Answers

There is nothing stopping you from creating your own deserializer. A very naive example could be the following:

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss+00:00";

    private final DateTimeFormatter formatter;

    public LocalDateTimeDeserializer() {
        this.formatter = DateTimeFormatter.ofPattern(PATTERN);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        return LocalDateTime.parse(p.getText(), formatter);
    }
}

The only thing you need to notice is that you'll need to escape the 'T' by adding single quote around it.

With the deserializer in place you can simply annotate the field like so:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime dateTime;
like image 159
akortex Avatar answered Oct 21 '25 04:10

akortex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!