How do I properly add a 'Z' at the end of a datetime that's marshalled by Spring MVC with JAXB?



I have a DateTimeXmlAdapter that I'm using to convert Joda dates to Strings. It looks as follows:



public class DateTimeXmlAdapter extends XmlAdapter<String, DateTime> {
private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ";
private static final DateTimeFormatter formatter = DateTimeFormat.forPattern(PATTERN);

@Override
public DateTime unmarshal(String value) {
return formatter.parseDateTime(value);
}

@Override
public String marshal(DateTime value) {
return formatter.print(value);
}
}


This results in the following string in XML:



2014-10-16T18:31:57-0400


However, the endpoint I'm sending this to expects a 'Z' at the end instead of -0400. They recommend converting to UTC so timezone information doesn't need to be sent. Makes sense to me. However, I can't seem to get a 'Z' to be at the end unless I state it literally. As in:



private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";


The discussion below seems to indicate that adding the Z is not recommended.


Validating Timestamp format yyyy-MM-dd'T'HH:mm:ssZ in java?


It tried changing it to the following, but this results in a date w/o the 'Z'.



public class DateTimeXmlAdapter extends XmlAdapter<String, DateTime> {
private static final DateTimeFormatter formatter = ISODateTimeFormat.dateHourMinuteSecond().withZoneUTC();

@Override
public DateTime unmarshal(String value) {
return formatter.parseDateTime(value);
}

@Override
public String marshal(DateTime value) {
return formatter.print(value);
}
}


Output:



2014-10-17T18:50:43


The endpoint I'm talking to says they need the 'Z' because it indicates zero offset from UTC. What's the proper way to add a 'Z' at the end when using Joda Time and JAXB?


Thanks,


Matt


No comments:

Post a Comment