how replace XmlGregorianCalendar by Date?
jax-ws, web-services
Solution
Not tested, but should work. First create such class:
import javax.xml.bind.DatatypeConverter;
public class XsdDateTimeConverter {
public static Date unmarshal(String dateTime) {
return DatatypeConverter.parseDate(dateTime).getTime();
}
public static String marshalDate(Date date) {
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return DatatypeConverter.printDate(calendar);
}
public static String marshalDateTime(Date dateTime) {
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(dateTime);
return DatatypeConverter.printDateTime(calendar);
}
}
Then add this to custom `xjb` file:
<javaType
name="java.util.Date"
xmlType="xs:dateTime"
parseMethod="XsdDateTimeConverter.unmarshal"
printMethod="XsdDateTimeConverter.marshalDateTime"
/>
<javaType
name="java.util.Date"
xmlType="xs:date"
parseMethod="XsdDateTimeConverter.unmarshal"
printMethod="XsdDateTimeConverter.marshalDate"
/>
</globalBindings>
Not tested, but should work. Based on my answer here: JAX-WS and Joda-Time?
Problem
I have to expose an ejb service layer via jax-ws . I have generated the web service using jax-ws and wsimport but I'm stopped by a strange things ; Date are being mapped to XmlGregorianCalendar . Is it possible to use classic java Date instead ? Can you show me the right way to proceed ? Thanks . Edit: this the binding file i used : thanks , I modified slightly your xml and attached it with netbeans to the client's webservice and it worked . This the binding I used : ``` <jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" wsdlLocation="../wsdl/localhost_8080/web_test/Testor.wsdl" > <jaxb:globalBindings> <jaxb:javaType name="java.util.Date" xmlType="xsd:dateTime" parseMethod="lol.XsdDateTimeConverter.unmarshal" printMethod="lol.XsdDateTimeConverter.marshalDateTime" /><jaxb:javaType name="java.util.Date" xmlType="xsd:date" parseMethod="lol.XsdDateTimeConverter.unmarshal" printMethod="lol.XsdDateTimeConverter.marshalDate" /> </jaxb:globalBindings> </jaxws:bindings> ```