There are a number of ways to do this. The two major choices are
1) A SimpleDateFormat
2) String.format() using a Format String.
-- You can also use this with System.out.printf() if that is all you need to do
Note that both options use a Date object, which you can pull out of the Calendar.
Joined: Aug 19, 2010
Indeed, I thought the "2011-10-24T06:37:12.561Z" example was some kind of global standard.
Cesar Coz wrote:Indeed, I thought the "2011-10-24T06:37:12.561Z" example was some kind of global standard.
Yes, that's ISO 8601. There's still variation permissible within that standard though, so there's no one ISO 8601 setting available in Java. You craft the format you want, using SimpleDateFormat.
Cesar Coz wrote:
Good approach, but there are still some problems. Most importantly, although you append a literal Z which makes it look like GMT (Zulu time), it's not. Unless you happen to be running on a JVM with GMT (or an equivalent zone) as its default. Even though the GregorianCalendar was using GMT, that information is forgotten when you extract a Date object. Which is fine - but you need to tell the DateFormatter what time zone you want to use, E.g.
Additionally, using 'kk' in the format string gives you hour of day in the range 1-24, not 0-23. This means that 30 minutes after midnight will be shown as 24:30 rather than 00:30. I don't know which you prefer, but ISO 8601 would normally use 00:30. For this, I would replace the kk with HH instead, for normal -23 hours.
Cesar Coz wrote:Does anyone know how to parse the GregorianCalendar to that format using UTC Time Zone ?
Very minor nit: in normal discussions, what you are trying to do is format the Calendar/Date object. The word "parse" is usually used to take a string and convert it to a Date/Calendar object. If you read the Javadocs for SimpleDateFormat, you will see this usage.