1

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 like:

<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,

Telegard
  • 137
  • 3
  • 12
  • This answer http://stackoverflow.com/questions/4687188/how-to-convert-dateactionscript-3-to-java-util-date-through-a-xml/4694496#4694496 is what you need (use a XmlAdapter) –  Aug 22 '11 at 05:07
  • Thanks RC, thank link was perfect, worked like a charm. – Telegard Aug 22 '11 at 23:34
  • Quick update on the timeZone conversion issue, it was related to the recordset.getDate("field") method I was using to pull the date from the database. Adding a Calendar object set to UTC and the using recordset.getDate("field",cal) mitigated the timezone conversion. Cheers! – Telegard Aug 22 '11 at 23:36

1 Answers1

2

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.

Femi
  • 64,273
  • 8
  • 118
  • 148