Changing Date format output by javax.xml
I have a Tomcat server app, which creates xml responses based on classes I populate.
One of the classes has a date field:
@XmlRootElement
public class Login {
private String mLoginUserID;
private String mLoginPassword;
private Date mRecordChangeDateTime;
...
@XmlElement(name = "recordChangeDateTime")
public Date getRecordChangeDateTime() {
return mRecordChangeDateTime;
}
The resulting XML output looks l开发者_JAVA百科ike:
<recordChangeDateTime>2011-08-02T21:03:00-04:00</recordChangeDateTime>
Couple problems I am trying to figure out...
1) Its converting the date to local timezone, the date is handled as UTC all the way throughout, but when javax.xml outputs it, it applies the timezone conversion.
2) I am trying to configure how the date is formatted. My standard format is "yyyy-MM-dd HH:mm:ss" across my client devices, and would like the xml response to use this format as well.
I having been spending many many hours researching and trying to work through this, I have attempted lots of variations of SimpleDateFormat, XMLGregorianCalendar, etc... but nothing I do changes the output in anyway... I am not sure if there is an annotation or something else that allows me to configure the date output programmatically?
Any insights would be greatly appreciated! Thanks,
This is ISO8601 format.
You want to use XmlJavaTypeAdapter, as outlined here. This will let you specify the XmlAdapter that handles all type conversions for that element. You can provide a custom one that parses/formats dates however you'd like.
精彩评论