Hooli
Hooli

Reputation: 1195

jackson datetime pattern not displaying timezone correctly

My class:

@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", timezone="UTC")
protected XMLGregorianCalendar dateTime;

My date instance:

XMLGregorianCalendar dateTime = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-04-10T15:27:37+01:00");
obj.setDateTime(dateTime);

The translation:

ObjectMapper xmlMapper = new XmlMapper();
xmlMapper.setSerializationInclusion(Include.NON_NULL);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xmlMapper.writeValueAsString(object);

The output:

<DateTime>2013-04-10T14:27:37+0000</DateTime>

Expected output:

<DateTime>2013-04-10T15:27:37+01:00</DateTime>

Why is the timezone and timezone format getting lost in translation?

Upvotes: 0

Views: 670

Answers (1)

Abbel
Abbel

Reputation: 320

You are setting the Timezone for Jackson to UTC. See your annotation: @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", timezone="UTC")
I think the output is exactly what you would expect since UTC has the timezoneoffset +0000

Upvotes: 1

Related Questions