Reputation: 11
i am trying to convert from string to XML Gregorian Calendar date and its always returning me a new instance, Please check my code below
Input date ::2014-03-13 15:34:33 +0000
Desired Output date :: 2014-03-13 15:34:33 +0000
But when i convert using my code below the output is 2014-03-13T11:34:33.000-04:00
public static void convertXMLGregorian() throws DatatypeConfigurationException {
TimeZone utc = TimeZone.getTimeZone("UTC");
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
XMLGregorianCalendar xc=DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
String xcs = df.format(xc.toGregorianCalendar().getTime());
XMLGregorianCalendar converted = stringToXMLGregorianCalendar(xcs, df);
System.out.println("converted " + converted);
// output :: 2014-03-13T11:34:33.000-04:00
// String conversion methods
private static XMLGregorianCalendar dateToXMLGregorianCalendar(Date date) {
try {
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar
.getInstance();
gc.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (DatatypeConfigurationException e) {
System.out.print(e.getMessage());
return null;
}
}
private static XMLGregorianCalendar stringToXMLGregorianCalendar(
String datetime, SimpleDateFormat sdf) {
try {
Date date = sdf.parse(datetime);
return dateToXMLGregorianCalendar(date);
} catch (ParseException e) {
System.out.print(e.getMessage());
return null;
}
Please Help, Thanks in advance.
Upvotes: 1
Views: 11247
Reputation: 1503439
See below for why the existing code isn't working for you, and a workaround. However, to preserve the UTC offset and do the whole thing far more simply, I suspect you just want to use DatatypeFactory.newXMLGregorianCalendar(String)
:
// This replaces *all* of the code in the question
XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar(datetime);
The only fly in the ointment here is that it looks like your input doesn't have a T
in it, whereas the canonical XML representation does. That's easily fixed:
// The only place you've got a space is exactly where you want a T.
datetime = datetime.replace(' ', 'T');
XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar(datetime);
Original answer
I suspect that this is the problem:
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
That will create a calendar in the default time zone.
If you always want to have a UTC offset of 0, just use:
gc.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
If you're trying to preserve the UTC offset in the original text, that's a different matter - you can't do that with your current approach, because Date
doesn't maintain that information. (It's just an instant in time.)
As an aside, I would strongly suggest that you change your exception handling strategy - simply returning null
and continuing as if all was well is rarely a good idea. It's usually a better idea to either let the exception propagate or wrap it in a more suitable exception type (possibly a runtime exception).
Upvotes: 4
Reputation: 38152
System.out.println("converted " + converted);
This will use the toString() method of converted. You might want to use your SimpleDateFormat here?
Upvotes: 0