5

The time that I am getting is in the format "2011-07-31T08:16:37.733Z". Actually the Z should be the timezone and then that time is converted to local timezone. How do I actually convert this time to default timezone now.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
sunil
  • 9,541
  • 18
  • 66
  • 88

2 Answers2

13

RFC 3339 describes a specific ISO 8601 'profile' for date/time representation.

If using 3rd-party open-source libraries is an option, take a look at Joda-Time's ISODateTimeFormat utility class.

Otherwise, if you need to roll out your own code it's not enough to use a SimpleDateFormat because of the way ISO 8601/RFC 3339 represents timezone information.

If all you need to parse is in 'Z' (Zulu or UTC), then it's relatively simple:

String input = "2011-08-11T01:23:45.678Z";
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
f.setTimeZone(utc);
GregorianCalendar cal = new GregorianCalendar(utc);
cal.setTime(f.parse(input));
System.out.println(cal.getTime());

Should print out "2011-08-11T01:23:45.678Z" in your local time zone.

If you need to handle arbitrary timezones in RFC 3339 then you'll need to extend the above to parse out the timezone designator and retrieve the corresponding Java TimeZone instance. That shouldn't be too hard if it's just whole hours +/- GMT, but in case of non-standard offsets you may have to create your own SimpleTimeZone instance.

Alistair A. Israel
  • 6,417
  • 1
  • 31
  • 40
0
private static SimpleDateFormat DATE_TIME_FORMAT=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

DATE_TIME_FORMAT.setTimeZone(TimeZone.getID()); //for example
date=DATE_TIME_FORMAT.format(curDate);
atzu
  • 424
  • 3
  • 14
  • I have mentioned the format that the date is coming. That date is in RFC3339 format. If you have any idea of how to parse RFC3339 dates then please let me know. – sunil Aug 10 '11 at 12:12
  • Edited above, was it helpful? This might help: http://cokere.com/RFC3339Date.txt – atzu Aug 10 '11 at 12:55