Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify the date format used when JAXB marshals xsd:dateTime?

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); 
like image 982
Young Fu Avatar asked Nov 26 '12 16:11

Young Fu


People also ask

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

What is JAXB marshalling?

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.

What is JAXB context?

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


2 Answers

You can use an XmlAdapter to customize how a date type is written to XML.

package com.example;  import java.text.SimpleDateFormat; import java.util.Date;  import javax.xml.bind.annotation.adapters.XmlAdapter;  public class DateAdapter extends XmlAdapter<String, Date> {      private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      @Override     public String marshal(Date v) throws Exception {         synchronized (dateFormat) {             return dateFormat.format(v);         }     }      @Override     public Date unmarshal(String v) throws Exception {         synchronized (dateFormat) {             return dateFormat.parse(v);         }     }  } 

Then you use the @XmlJavaTypeAdapter annotation to specify that the XmlAdapter should be used for a specific field/property.

@XmlElement(name = "timestamp", required = true)  @XmlJavaTypeAdapter(DateAdapter.class) protected Date timestamp;  

Using a xjb binding file:

<xjc:javaType name="java.util.Date" xmlType="xs:dateTime"         adapter="com.example.DateAdapter"/> 

will produce the above mentioned annotation.
(By eventually adding the xjc namespace: xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc")

like image 161
bdoughan Avatar answered Sep 22 '22 09:09

bdoughan


I use a SimpleDateFormat to create the XMLGregorianCalendar, such as in this example:

public static XMLGregorianCalendar getXmlDate(Date date) throws DatatypeConfigurationException {     return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd").format(date)); }  public static XMLGregorianCalendar getXmlDateTime(Date date) throws DatatypeConfigurationException {     return DatatypeFactory.newInstance().newXMLGregorianCalendar(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(date)); } 

The first method creates an instance of XMLGregorianCalendar that is formatted by the XML marshaller as a valid xsd:date, the second method results in a valid xsd:dateTime.

like image 30
Andrea Luciano Avatar answered Sep 19 '22 09:09

Andrea Luciano