I am using java xml annotations like this:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "OrganisationUnit", propOrder = {
"companyId",
"validFrom",
"validTo",
})
public class OrganisationUnit {
@XmlElement(name = "company_id", required = true)
protected String companyId;
@XmlElement(name = "valid_from")
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar validFrom;
@XmlElement(name = "valid_to")
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar validTo;
...
}
However when I make the information available via a REST service, the date
elements are displayed with time zones:
<organisationUnits>
<organisationUnit>
<company_id>ABC</company_id>
<valid_from>1970-01-01+01:00</valid_from>
<valid_to>2099-12-31+01:00</valid_to>
</organisationUnit>
</organisationUnits>
I don't fully understand whether the XmlSchemaType date
is required to show a time zone or not - but ideally I would like to show the dates without timezones.
<valid_from>1970-01-01</valid_from>
<valid_to>2099-12-31</valid_to>
How can I do this?
Well, reading the documentation sometimes tells you wonderful things, and here's exactly how to do it, with time zone DatatypeConstants.FIELD_UNDEFINED
:
private XMLGregorianCalendar dateWithoutTimezone(Date date) throws DatatypeConfigurationException {
calendar.setTime(date);
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
xmlGregorianCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
return xmlGregorianCalendar;
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With