I'm trying to get Jackson to parse the date-time values given to me by a particular API which I have no ability to change.
So I'm giving it a custom pattern:
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;
}
}
I get this exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mon, 14 Oct 2019 13:00:00 +0900' could not be parsed at index 0
It seems like it's failing at index 0, but I don't understand why because for the day of the week format pattern "E", it seems like "Mon" should be the correct value.
Tried so far:
Setting
DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE:mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false)Result: same exception.
Adding
Jdk8Module:ObjectMapper mapper = new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new JavaTimeModule());Result: same exception.