• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem parsing time

 
Cesar Coz
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a GregorianCalendar time that comes from a web service, when I print it i get it in this format:

java.util.GregorianCalendar[time=1359074696474,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=24,DAY_OF_YEAR=24,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=44,SECOND=56,MILLISECOND=474,ZONE_OFFSET=-18000000,DST_OFFSET=0]

and I need it in this format:

2011-10-24T06:37:12.121Z

Does anyone know how to parse the GregorianCalendar to that format using UTC Time Zone ?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cesar Coz
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, I thought the "2011-10-24T06:37:12.561Z" example was some kind of global standard.

I used this simple solution



Thanks for your reply !
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

or

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.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cesar Coz
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you both are right! thanks for the advices!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic