【发布时间】:2021-04-29 07:37:12
【问题描述】:
尝试使用 json 文件中的特定模式反序列化日期。
我想从 json 文件接收的对象:
@Data
public class MyClass {
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'UTC'")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime date;
}
Json 文件:
{
"date" : "2017-01-01T00:00:59.000UTC"
}
代码示例我想如何接收它:
ObjectMapper mapper = new ObjectMapper();
MyClass clazz = mapper.readValue(new File("MyFile.json"), MyClass.class);
实际结果:
com.fasterxml.jackson.databind.exc.InvalidFormatException:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2017-01-01T00:00:59.000UTC":
Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException)
Text '2017-01-01T00:00:59.000UTC' could not be parsed, unparsed text found at index 23
at [Source: (File); line: 2, column: 11] (through reference chain: com.example.MyClass["date"])
如何反序列化当前的日期模式?
【问题讨论】:
-
2017-01-01T00:00:59.000UTC是一种特殊的混合格式。国际标准ISO 8601有什么问题?