简体   繁体   中英

Spring boot: JSON deserialize date and time with time zone to LocalDateTime

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"])]

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.

在此处输入图像描述

Change your declaration as follows:

private OffsetDateTime startTime;

2. Use of wrong format

You need to use XXX for the offset part ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM