I'm generating an XML which contains a date in a valid XML
format, and I need it to include a UTC
offset as well.
I'm ussing groovy
but I'll show the Java
code I'm using instead (an answer in either language is good):
Calendar c = Calendar.getInstance();
long timeZoneOffset = c.timeZone.getOffset(c.getTimeInMillis())/(1000*60*60);
SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
formatter.format(c.getTime()) + "+0" + timeZoneOffset + ":00";
The above code give4s me 2011-06-12T07:23:25.000+03:00
, but this code has two problems:
- It is ugly, and probably not the best way to do this
- It won't work for timezones like India (GMT +5:30), Nepal (GMT +5:45)
I tried using new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z")
for the timezone, but it gave me 2011-06-12T07:23:25.000+0300
which is not a correct format (+0300
instead of +03:00
).
Any other way to format the date the way I need it? (preferably without 3rd parties)