-
Notifications
You must be signed in to change notification settings - Fork 123
Description
Hello, i have an issue regarding the serialization of OffsetDateTime, the timezone i set in the object mapper is not used
With the following code
public class POJO {
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
private OffsetDateTime offsetDateTimeWithoutTimeZone;
}
public static void setUp() {
mapperWithTimezone = new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.setTimeZone(TimeZone.getTimeZone("Europe/Paris"))
.enable(SerializationFeature.INDENT_OUTPUT);
}
final OffsetDateTime firstJune2020AtNoonOffsetUTC = OffsetDateTime.of(
LocalDate.of(2020, Month.JUNE, 1),
LocalTime.NOON,
ZoneOffset.UTC);
final POJO pojoOffsetUTC = new POJO();
pojoOffsetUTC.setOffsetDateTimeWithoutTimeZone(firstJune2020AtNoonOffsetUTC);
final String strPojoOffSetUTC = mapperWithTimezone.writeValueAsString(pojoOffsetUTC);
I'm expecting that the output is
{"offsetDateTimeWithoutTimeZone" : "2020-06-01T14:00:00.000+02:00"}
but i get
{"offsetDateTimeWithoutTimeZone" : "2020-06-01T12:00:00.000Z"}
The javadoc of com.fasterxml.jackson.annotation.JsonFormat#timezone tells me that the default is the one specified in the objectMapper, but UTC is used instead.
I search a bit in the code, and i didn't see anything regarding the use of ObjectMapper#setTimezone in the method com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#createContextual
When i specify the timezone in the JsonFormat annotation, it work as intended, but i would prefer to have a global setting rather specifying it for each field.
I created a repo with a working code to illustrate my question : https://github.com/ErwanLeroux/jackson-issue