3

I'm using Java 11, Spring Boot 2.2.6.RELEASE. How can I deserialize "2019-10-21T13:00:00+02:00" to LocalDateTime?

Tried so far:

  @JsonSerialize(using = LocalDateTimeSerializer.class)
  @JsonDeserialize(using = LocalDateTimeDeserializer.class)
  @DateTimeFormat(iso = ISO.DATE_TIME)
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
  private LocalDateTime startTime;

But I get the following error:

2021-02-19 07:45:41.402  WARN 801117 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19
 at [Source: (PushbackInputStream); line: 2, column: 18] (through reference chain: example.app.dto.DtoRequest["startTime"])]
Michael
  • 41,989
  • 11
  • 82
  • 128
tomi
  • 373
  • 1
  • 5
  • 16
  • Does this answer your question? [Deserialize Java 8 LocalDateTime with JacksonMapper](https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper) – Roar S. Feb 19 '21 at 13:01
  • The value shown contains a time zone offset, and should be mapped to an `OffsetDateTime`, not a `LocalDateTime`. – Mark Rotteveel Feb 19 '21 at 17:33
  • 1
    @RoarS. - The duplicate target that you have mentioned doesn't answer this question. Please check the answer below for details. – Arvind Kumar Avinash Feb 19 '21 at 18:08

1 Answers1

10

There are two problems with your code:

1. Use of wrong type

Cannot deserialize value of type java.time.LocalDateTime from String "2019-10-21T13:00:00+02:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2019-10-21T13:00:00+02:00' could not be parsed, unparsed text found at index 19

If you analyze the error message, you will find that it is clearly telling you that there is some problem at index 19.

2019-10-21T13:00:00+02:00
// index 19 ---->  ^  

And, the problem is that LocalDateTime does not support timezone. Given below is an overview of java.time types and you can see that the type which matches with your date-time string is OffsetDateTime because it has a zone offset of +02:00 hours.

enter image description here

Change your declaration as follows:

private OffsetDateTime startTime;

2. Use of wrong format

You need to use XXX for the offset part i.e. your format should be uuuu-MM-dd'T'HH:m:ssXXX. If you want to stick to Z, you need to use ZZZZZ. Check the documentation page of DateTimeFormatter for more details.

Demo:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2019-10-21T13:00:00+02:00";
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:m:ssXXX");
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtf);
        System.out.println(odt);
    }
}

Output:

2019-10-21T13:00+02:00

Learn more about the modern date-time API from Trail: Date Time.

Also related, RFC3339 - Date and Time on the Internet: Timestamps

This document defines a date and time format for use in Internet
protocols that is a profile of the ISO 8601 standard for
representation of dates and times using the Gregorian calendar.

Community
  • 1
  • 1
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110