Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I represent dates without the timezone using Apache CXF?

I have a WSDL that specifies an element's type to be xs:date.

When I use Apache CXF to generate the Java classes, it renders the variable as an javax.xml.datatype.XMLGregorianCalendar (all good so far).

When CXF renders an XML document containing this, it renders it in this form (where -06:00 represents the time zone):

2000-01-18-06:00

How can I configure CXF not to render the timezone?

like image 771
Jared Avatar asked May 26 '11 18:05

Jared


2 Answers

GregorianCalendar gcal = new GregorianCalendar();
start = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
start.setTimezone(DatatypeConstants.FIELD_UNDEFINED);

Don't ask me why in every bit of the sane logic - when marshalling the XMLgregorianCalendar to xs:date it retains the time zone.

I always thought - the time zone could be more applicable to xs:dateTime, but what I know ... about types.

For me, it doesn't make sense to have the time zone by default, for a xs:date type and this is a problem in the marshalling logic.

like image 81
Filip Avatar answered Sep 22 '22 09:09

Filip


To complete Filip answer (thanks to him!), maybe it will help some of you ...

I had to declare a new XmlAdapter on the concern field date with the annotation @XmlJavaTypeAdapter

public class YourDTO {
   // ... 
   @XmlElement
   @XmlSchemaType(name = "dateTime")
   @XmlJavaTypeAdapter(type = XMLGregorianCalendar.class, value = XmlDateAdapter.class)
   public Date yourDate;
   // ...
}

The adapter

public class XmlDateAdapter extends XmlAdapter<XMLGregorianCalendar, Date> {

@Override
public XMLGregorianCalendar marshal(Date date) throws Exception {
    GregorianCalendar gcal = new GregorianCalendar();
    gcal.setTime(date);
    XMLGregorianCalendar xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
    xmlDate.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    return xmlDate;
}
// ...

SOAP message date format before

2017-04-18T00:00:00+02:00

SOAP message date format after

2017-04-18T00:00:00

like image 37
Hamarkhis Avatar answered Sep 19 '22 09:09

Hamarkhis