Reputation: 369
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
Upvotes: 1
Views: 2047
Reputation: 148977
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:
Upvotes: 1
Reputation: 5715
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.
Upvotes: 1
Reputation: 1259
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());
Upvotes: 0