Date to XMLGregorianCalendar

XMLGregorianCalendar is a representation for W3C XML Schema 1.0 date/time datatypes. The XML Schema standard specifies very precise rules about the functionality and representation of dates in XML which is doesn't match existing date classes in the JDK. This example will show how to convert a date to a XMLGregorianCalendar.

Straight up Java

@Test
public void convert_date_to_XMLGregorianCalendar()
        throws DatatypeConfigurationException {

    GregorianCalendar gCalendar = new GregorianCalendar();

    XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory
            .newInstance().newXMLGregorianCalendar(gCalendar);

    logger.info(xmlGregorianCalendar);

    assertNotNull(xmlGregorianCalendar);
}

Output

2013-10-21T19:55:00.048-05:00