I'm using JAXB 2.2.8-b01 impl and I have a schema which has a xs:date element which creates a XMLGregorianCalendar instance. I'm trying to get a Joda-Time DateTime timestamp format but since I have to have a XMLGregorianCalendar instance, I'm not sure its possible. Any ideas?
Schema XSD:
<xs:element type="xs:date" name="date-archived" minOccurs="0" maxOccurs="1" nillable="false"/>
JAXB Generated Property:
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar date;
XML Conversion Class:
//java.util.Date being passed
private XMLGregorianCalendar converToGregorianCal(Date date) {
DatatypeFactory df = null;
try {
df = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException e) {
LOG.error("error getting DatatypeFactory instance " + e.getMessage());
}
if (date == null) {
return null;
} else {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return df.newXMLGregorianCalendar(gc);
}
}
The Java XMLGregorianCalendar class, introduced in Java 1.5, is a representation of the W3C XML Schema 1.0 date/time datatypes and is required to use the XML format.
datatype. DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .
Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat. Low level - builder.
This is a short way:
public DateTime convert(final XMLGregorianCalendar xmlgc) {
return new DateTime(xmlgc.toGregorianCalendar().getTime());
}
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