Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping Java Date Object to XML Schema datetime format

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?

like image 656
Mark Estrada Avatar asked Apr 25 '11 06:04

Mark Estrada


People also ask

What is the dateTime format in XML?

The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss" where: YYYY indicates the year. MM indicates the month.

How do I change the date format in a schema?

You cant change the date type to a specific format but you can take it as a string and choose a specific format that you want.

Which of the following is the correct representation of date and time in XML schema?

The correct answer is A (xs:date) and C (xs:gMonthDay). These two are embedded simple types representing time and date under XML Schema.

What is the format of dateTime?

For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".


2 Answers

You can do the following to control the schema type:

@XmlElement
@XmlSchemaType(name="date")
private Date period;

For More Information:

  • http://bdoughan.blogspot.com/2011/01/jaxb-and-datetime-properties.html
like image 82
bdoughan Avatar answered Oct 06 '22 01:10

bdoughan


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.

like image 39
Vladimir Dyuzhev Avatar answered Oct 06 '22 01:10

Vladimir Dyuzhev