Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joda DateTime Format for XMLGregorianCalendar Type

Tags:

java

xml

jaxb

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);
    }
}
like image 962
c12 Avatar asked Jan 22 '14 22:01

c12


People also ask

What is the format of XMLGregorianCalendar?

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.

How do you convert GregorianCalendar to XMLGregorianCalendar?

datatype. DatatypeFactory object can convert from GregorianCalendar to XMLGregorianCalendar by calling its newXMLGregorianCalendar method. XMLGregorianCalendar xmlGregCal = DatatypeFactory . newInstance() .

What is Joda-Time format?

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.


1 Answers

This is a short way:

public DateTime convert(final XMLGregorianCalendar xmlgc) {
    return new DateTime(xmlgc.toGregorianCalendar().getTime());
}
like image 59
thomas.mc.work Avatar answered Oct 08 '22 18:10

thomas.mc.work