Based on this Java Date Time - OffsetDateTime.format() Examples article and this official documentation for DateTimeFormatter I would expect my OffsetDateTime to be serialized as 2011-12-03T10:15:30+00:00
where the offset the offset is +00:00
since its UTC.
I cannot manage to get the OffsetDateTime to render with an offset, it always just renders with the 'Z' Zulu. What am I doing wrong?
This is Spring Boot 2.0.0.RELEASE, and as you can see in the screenshot I've got the following modules on the class path and registered with the objectMapper, although I don't think that's relative because this issue seems to be directly with the DateTimeFormatter, my object mapper is just using the formatter I am giving it.
It does have an affect, because as you can see in the second screenshot when I specify the BASIC_ISO_FORMAT
it does produce a a different result.
I do have this property set spring.jackson.date-format= com.fasterxml.jackson.databind.util.ISO8601DateFormat
in my application.properties but as I understand it, this has no affect on OffsetDateTime it only supports the legacy Date objects from previous version of java. Incidentally changing this seems to have no affect as expected.
Any help would be appreciated.
Using ISO_ZONED_DATE_TIME format...
Using BASIC_ISO_FORMAT... this does have an affect so I know the formatter is doing something, I'm just not clear why the ISO_ZONED_DATE_TIME is not rendered as expected.
this.objectMapper = objectMapperBuilder.build()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.disable(SerializationFeature.INDENT_OUTPUT)
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer<OffsetDateTime>() {
@Override
public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
String formattedDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
jsonGenerator.writeString(formattedDate);
}
});
this.objectMapper.registerModule(simpleModule);
OffsetDateTime now = OffsetDateTime.now();
TimeZone defaultTimeZone = TimeZone.getDefault();
ZoneId defautlZoneOffset = ZoneOffset.systemDefault();
String serializedOffsetDateTime = this.objectMapper.writeValueAsString(now);
//returns -> "2018-06-27T11:45:56.035Z"