Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlSchemaType "date" - without time zone?

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?

like image 260
vikingsteve Avatar asked Oct 03 '14 11:10

vikingsteve


1 Answers

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.

like image 117
vikingsteve Avatar answered Nov 01 '22 18:11

vikingsteve