I am creating XML that contains a date in a valid format XML, and I need it to include an offset as well UTC.
I use groovy, but I will show the code Javathat I use (the answer in any 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 gives 4s me 2011-06-12T07:23:25.000+03:00, but this code has two problems:
- This is ugly and probably not the best way to do this.
- It will not work for time zones such as India ( GMT +5: 30 ), Nepal ( GMT +5: 45 )
I tried using new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss Z")for the time zone, but it gave me 2011-06-12T07:23:25.000+0300which is not the correct format ( +0300instead +03:00).
Any other way to format the date the way I need it? (preferably without third parties)
source
share