【发布时间】:2019-10-24 08:44:00
【问题描述】:
我正试图让 Jackson 解析由我无法更改的特定 API 提供给我的日期时间值。
所以我给它一个自定义模式:
public class Problem {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
String json = "{\"posted_at\":\"Mon, 14 Oct 2019 13:00:00 +0900\"}";
Model model = mapper.readValue(json, Model.class);
System.out.println(model.posted_at);
}
public static class Model {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "E, d MMM yyyy HH:mm:ss XX")
public ZonedDateTime posted_at;
}
}
我得到了这个例外:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 14 Oct 2019 13:00:00 +0900' could not be parsed at index 0
它似乎在索引 0 处失败,但我不明白为什么,因为对于星期格式模式“E”,“Mon”似乎应该是正确的值。
到目前为止尝试过:
-
设置
DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE:mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false)结果:同样的异常。
-
添加
Jdk8Module:ObjectMapper mapper = new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule());结果:同样的异常。
【问题讨论】: