Jackson / JavaTimeModule - handle different clockwise offset formats

I am calling an API (using Jersey) that returns Date Time with zone offset. Sometimes the data is in this format:

"2017-03-28T14:40:00+01:00"

      

and sometimes it is in this format (I have no control over this)

"2017-03-28T14:40:00+0100"  (where the ':' in timezone offset is missing).

      

I want to combine them into objects java.time.ZonedDateTime

. I use JavaTimeModule()

in my jersey ObjectMapper

.

objectMapper.registerModule(new JavaTimeModule());

      

Question : is there a way to make make mapperper flexible enough to handle timezone offset in +01:00

or +0100

?

+3


source to share


1 answer


You can specify a template with optional sections (delimited []

) to indicate that the offset can have two different formats and add them to the appropriate field using an annotation @JsonFormat

.

I created this test class:

public class SampleType {

    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss[XXX][XX]")
    private ZonedDateTime date;

    // getter and setter
}

      

Note the last part ( [XXX][XX]

): each pair []

is an optional sector, so the parser tries to parse each one if present. XXX

is the offset with :

and XX

is the offset without it (see javadoc for more details )

With this one can read both formats:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

// offset with ":"
String json = "{ \"date\": \"2017-03-28T14:40:00+01:00\" }";
SampleType value = mapper.readValue(json, SampleType.class);
System.out.println(value.getDate()); // 2017-03-28T13:40Z[UTC]

// offset without ":"
json = "{ \"date\": \"2017-03-28T14:40:00+0100\" }";
value = mapper.readValue(json, SampleType.class);
System.out.println(value.getDate()); // 2017-03-28T13:40Z[UTC]

      




Please note that the resulting value is ZonedDateTime

converted to UTC:2017-03-28T13:40Z[UTC]

If you want to keep the original offset, just use the class com.fasterxml.jackson.databind.DeserializationFeature

to customize ObjectMapper

:

// add this to preserve the same offset (don't convert to UTC)
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false);

      

In this case, the offset is saved (the value is not converted to UTC), and the output for the above tests will be 2017-03-28T14:40+01:00

.

0


source







All Articles