我有一个通过JSON输入的日期字符串:“29-OCT-21 12.00.00.000000000 AM UTC”。我想保存它作为ZonedDateTime数据类型。
我在模型中设置了如下属性:
@JsonProperty("createdTs")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MMM-yy hh.mm.ss.SSSSSSSSS a z", with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
private ZonedDateTime createdTs;
字符串
我得到一个错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.ZonedDateTime` from String "29-OCT-21 12.00.00.000000000 AM UTC": Failed to deserialize java.time.ZonedDateTime: (java.time.format.DateTimeParseException) Text '29-OCT-21 12.00.00.000000000 AM UTC' could not be parsed
型
我不知道哪里出了问题。在测试用例中,模式在格式化程序中工作得很好,就像这样:
DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yy hh.mm.ss.SSSSSSSSS a z").toFormatter(
Locale.getDefault());
locationPayload.setcreatedTs(ZonedDateTime.parse("29-OCT-21 12.00.00.000000000 AM UTC", formatter));
型
1条答案
按热度按时间rjjhvcjd1#
此问题与
JsonFormat
无法处理JSON字符串中的复杂日期-时间格式有关。要解决此问题,您可以为ZonedDateTime类型创建自定义验证程序。下面是一个示例:
字符串
你的财产
型
测试
型
输出
Test(createdTs=2021-10-29T00:00Z[UTC])
试试看,然后告诉我