I hope this is not a repeat.
I checked other searches here and all of them seem to talk about “displaying” the date in the right TimeZone format using SimpleDateFormat.
However, my problem is I obtain an XMLGregorianCalendar Object which is let us say in “CET”.
I have to find out the format from this object and send the current time also in the same TimeZone as the server.
For eg: I need an XMLGregorianCalendar Object that returns me in this format(with Timezone):
2012-09-19T15:23:36.421+02:00
So I just tried this following snippet which seems to only return the time in local Timezone 🙁
TimeZone utc = TimeZone.getTimeZone("CET");
GregorianCalendar gc = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
df.setTimeZone(utc);
System.out.println(" - Gregorian UTC [" + df.format(gc.getTime()) + "]")
XMLGregorianCalendar currServTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
System.out.println("currServTime is "+currServTime);
You should include the time zone you’re interested in in the
GregorianCalendar, either by passing it to the constructor or by setting it afterwards. So either of these lines should work for you: