我需要解析LocalDateTime。我重新接收此String,例如:
2020-06-05T13:54:24+03:00
好的,我在这里看到带时移的日期时间。我添加了杰克逊数据类型库:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
我设置了ObjectMapper:
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule())
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
}
&我设置了反序列化模式:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
private LocalDateTime createDateTime;
或类似这样:
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX")
private LocalDateTime createDateTime;
但我重新接受此异常:
Caused by: java.time.format.DateTimeParseException: Text '2020-06-05T13:54:24+03:00' could not be parsed, unparsed text found at index 19
如果我以此方式解析此字符串:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
LocalDateTime dateTime = LocalDateTime.parse("2020-06-05T13:54:24+03:00", formatter);
一切都好
据我了解,问题在于时移。如何解决?