Mapping Java Date Object to XML Schema datetime format
Solution 1
You can do the following to control the schema type:
@XmlElement
@XmlSchemaType(name="date")
private Date period;
For More Information:
Solution 2
Use @XmlJavaTypeAdapter annotation and you can marshal/unmarshal your fields any way you want.
Cannot tell though if it's the simplest way.
And note also that it may harm interoperability with any code that would try to use your WSDL. The programmers for that other code would see xsd:string as the field type, and therefore will have to do formatting and parsing manually (just like you do, yes), introducing who knows how many bugs. So please consider if the xsd:date a bad choice really.
Stolen from here:
@XmlJavaTypeAdapter(value=DateAdapter.class, type=Date.class)
Date someDate;
...
public class DateAdapter extends XmlAdapter<String, Date> {
// the desired format
private String pattern = "MM/dd/yyyy";
public String marshal(Date date) throws Exception {
return new SimpleDateFormat(pattern).format(date);
}
public Date unmarshal(String dateString) throws Exception {
return new SimpleDateFormat(pattern).parse(dateString);
}
}
UPDATE: as was mentioned by @Blaise Doughan, a much shorter way is to annotate the date with
@XmlSchemaType("date")
Date someDate;
Despite it is still not clear why timezone information is not generated for the date, this code works in practice and requires much less typing.
Mark Estrada
Updated on July 31, 2020Comments
-
Mark Estrada almost 4 years
I am having some problem mapping my Java Data Type to standard Schema Date data type.
I have a simple class that I annotated like this. The period instance variable is of Java Date object type.
@XmlAccessorType(value = XmlAccessType.NONE) public class Chart { @XmlElement private double amount; @XmlElement private double amountDue; @XmlElement private Date period; //constructor getters and setters }
Here is my Web Service
@WebService public class ChartFacade { @WebMethod public Chart getChart() throws ParseException { SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); Chart chart = new Chart(20.0,20.5, df.parse("2001-01-01")); return chart; } }
My problem is it returns the date data in a format not according to what I am expecting.
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:getChartResponse xmlns:ns2="http://ss.ugbu.oracle.com/"> <return> <amount>20.0</amount> <amountDue>20.5</amountDue> **<period>2001-01-01T00:01:00+08:00</period>** </return> </ns2:getChartResponse> </S:Body> </S:Envelope>
I wanted the period element to be returned like this
<period>2001-01-01</period>
Is there any way I can achieve this?
-
Mark Estrada about 13 yearsThanks for your help. I think your suggestion would work, but data coming from DB is really a Date object and this class handles the mapping. If I convert this to string then I would probably have to recompile other code. Any other idea please?
-
qwerty about 13 years@road to yamburgs answer is much better. upvoting
-
bdoughan about 13 yearsFYI, JAXB's @XmlSchemaType("date") could be used for this use case: stackoverflow.com/questions/5775860/…
-
bdoughan about 13 years@road to yamburg - yyyy-MM-dd is the format of xsd:date, which is specified in the @XmlSchemaType annotation.
-
Vladimir Dyuzhev about 13 yearsGood to know, thanks, but can you point me to the documentation that says so? I looked at download.oracle.com/javaee/5/api/javax/xml/bind/annotation/… and it mentions no formats.
-
bdoughan about 13 years@road to yamburg - The JAXB @XmlSchemaType annotation allows you to specify the schema type, the format of the schema types is covered by the XML schema spec. The format for xsd:date is covered here: w3.org/TR/xmlschema-2/#date
-
Vladimir Dyuzhev about 13 yearsThe xsd:date date type has an optional timezone portion. Where in the XmsSchemaType documentation it says that timezone will not be spelled out in the generated XML?
-
bdoughan about 13 years@road to yamburg - Haven't found the spec reference yet, but neither the Metro (reference implementation) or MOXy include the namespace information when marshalling instances of java.util.Date to xsd:date.
-
Vladimir Dyuzhev about 13 yearsI took a look at a similar annotation in .NET world, and they format xsd:date the same way. Must be required in some sort of spec then. It is pretty interesting as a date without a timezone is about the same as text without an encoding. 2011-03-25 is 24+ different time points, in my understanding.