[英]Spring boot: JSON deserialize date and time with time zone to LocalDateTime
I'm using Java 11, Spring Boot 2.2.6.RELEASE.我正在使用 Java 11、Spring 启动 2.2.6.RELEASE。 How can I deserialize "2019-10-21T13:00:00+02:00" to LocalDateTime?如何将“2019-10-21T13:00:00+02:00”反序列化为 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:您的代码有两个问题:
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 19java.time.LocalDateTime
无法获得Z93F725A0742FE1C889F4.LOCALDATETIME的值的价值。 10-21T13:00:00+02:00' 无法解析,在索引 19 处找到未解析的文本
If you analyze the error message, you will find that it is clearly telling you that there is some problem at index 19.如果你分析错误信息,你会发现它清楚地告诉你索引 19 有问题。
2019-10-21T13:00:00+02:00
// index 19 ----> ^
And, the problem is that LocalDateTime
does not support timezone.而且,问题是LocalDateTime
不支持时区。 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.下面给出了 java.time 类型的概述,您可以看到与您的日期时间字符串匹配的类型是OffsetDateTime
,因为它的区域偏移量为+02:00
小时。
Change your declaration as follows:更改您的声明如下:
private OffsetDateTime startTime;
You need to use XXX
for the offset part ie your format should be uuuu-MM-dd'T'HH:m:ssXXX
.您需要将XXX
用于偏移部分,即您的格式应为uuuu-MM-dd'T'HH:m:ssXXX
。 If you want to stick to Z
, you need to use ZZZZZ
.如果你想坚持Z
,你需要使用ZZZZZ
。 Check the documentation page of DateTimeFormatter
for more details.查看DateTimeFormatter
的文档页面以获取更多详细信息。
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: Output:
2019-10-21T13:00+02:00
Learn more about the modern date-time API from Trail: Date Time .从Trail: Date Time了解有关现代日期时间 API 的更多信息。
Also related, RFC3339 - Date and Time on the Internet: Timestamps同样相关的还有RFC3339 - Internet 上的日期和时间:时间戳
This document defines a date and time format for use in Internet本文档定义了用于 Internet 的日期和时间格式
protocols that is a profile of the ISO 8601 standard for协议是ISO 8601标准的配置文件
representation of dates and times using the Gregorian calendar.使用公历表示日期和时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.