I am trying to parse the following date string received from a Bitbucket event payload:
2017-09-19T10:39:36+1000
When the incoming date is in offset +0000
then it works but +1000
does not.
This is a slightly non-standard (from the JDK's perspective) date string in that it has no colon in the offset. So I have made a custom DateTimeFormatter that works when the offset is +0000
:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffset("+HHMM", "+0000")
.optionalEnd()
.toFormatter();
However, when the offset is +1000
as above, it fails with:
Cannot deserialize value of type
java.time.OffsetDateTime
from String "2017-09-19T10:39:36+1000": Failed to deserialize java.time.OffsetDateTime: (java.time.format.DateTimeParseException) Text '2017-09-19T10:39:36+1000' could not be parsed at index 19
If the received date string is using offset +0000
then it works. How can I parse all time zones?
Update - Extra context:
This is being used to construct an instance of JavaTimeModule used to inform the ObjectMapper of the incoming date format:
JavaTimeModule javaTimeModule = new JavaTimeModule();
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart()
.appendOffset("+HHMM", "+0000")
.optionalEnd()
.toFormatter();
// This also fails:
// DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssx");
LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(formatter);
javaTimeModule.addDeserializer(LocalDateTime.class, deserializer);
// MAPPER is an instance of com.fasterxml.jackson.databind.ObjectMapper
MAPPER.registerModule(javaTimeModule);