1

We have a requirement to format the date fields differently during XML returned from jax-rs webservice.

ex.

Class Dates {
    Date date1; //in xml this date must be in format dd-mon-yyyy
    Date date2; // in xml this date must be in format dd-mm-yyyy hh:ss sss
} 

we tried XMLAdapter but we are not able to identify if it is for field date1 or date2

please advise. if there are any other filters or events I can use

Ravi
  • 329
  • 1
  • 5
  • 17
  • Duplicate here: http://stackoverflow.com/questions/11271375/gson-custom-seralizer-for-one-variable-of-many-in-an-object-using-typeadapter – antoniodvr Jan 19 '16 at 07:39

3 Answers3

1

You can use different xsd types for your dates. For the first use

<xs:element name="mySimpleDate" type="xs:date"/>

For th second use

<xs:element name="myDatetime" type="xs:dateTime"/>

in your xsd.

Hendrik Jander
  • 5,515
  • 3
  • 23
  • 31
1

we tried XMLAdapter but we are not able to identify if it is for field date1 or date2

An XmlAdapter is the right approach, you just need one per format you are trying to achieve.

@XmlAccessorType(XmlAccessType.FIELD)
class Dates {
    @XmlJavaTypeAdapter(MyDateAdapter.class)
    Date date1; //in xml this date must be in format dd-mon-yyyy

    @XmlJavaTypeAdapter(MyDateTimeAdapter.class)
    Date date2; // in xml this date must be in format dd-mm-yyyy hh:ss sss
} 

If you are generating these classes from an XML schema, see my answer linked below:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thanks for the example; I knew this is the answer but I was not able to bind the datatype. thanks very much for the help – Ravi Feb 04 '16 at 03:02
0

You can create a new class extending java.util.Date like a sort of wrapper:

public class CustomDate extends Date {

    public CustomDate() {
        super();
    } 

} 

Declare date2 as CustomDate and in this way you will be able to parse the two date correctly.

Otherwise declare date2 as Timestamp object and create two different adapter for both types.

To construct it starting from a Date object:

Date d2 = new Date();
Timestamp t2 = new Timestamp(d2.getTime());
antoniodvr
  • 1,259
  • 1
  • 14
  • 15
  • hmm... not happy the classes are generated via a XSD – Ravi Jan 18 '16 at 10:39
  • @Ravi What does it means? Can you change it or not? – antoniodvr Jan 18 '16 at 15:16
  • @Santoshi, I cannot make this change, as the xsd is obtained from the 3rd party vendor. Using this xsd and XJC command we are generating the jaxb beans. When we are returning the response they are expecting the dates in different format. As 3 such conventions it would not be feasible to search entire xsd and change data type every time XSD changes. I was thinking if I could use the events exposed by Marshaller – Ravi Jan 19 '16 at 03:11
  • If you cannot change the input xsd, you can try to [bind the elements to different datatypes](http://stackoverflow.com/questions/13633231/jaxb-overriding-datatype-of-an-element). The types can be changed on node or element level. The official guide is [here](https://jaxb.java.net/tutorial/index.html), with section 5.6.1 being the relevant one for you. – Hendrik Jander Jan 21 '16 at 16:45