When JAXB marshals a date object (XMLGregorianCalendar) into an xsd:dateTime element. How can you specify the format of the resulting XML?
For example:
The default data format uses milliseconds <StartDate>2012-08-21T13:21:58.000Z</StartDate>
I need to omit the milliseconds. <StartDate>2012-08-21T13:21:58Z</StartDate>
How can I specify the output form/date format that I want it to use?
I’m using javax.xml.datatype.DatatypeFactory to create the XMLGregorianCalendar object.
XMLGregorianCalendar xmlCal = datatypeFactory.newXMLGregorianCalendar(cal);
You can use an
XmlAdapterto customize how a date type is written to XML.Then you use the
@XmlJavaTypeAdapterannotation to specify that theXmlAdaptershould be used for a specific field/property.Using a xjb binding file:
will produce the above mentioned annotation.
(By eventually adding the
xjcnamespace:xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc")