aspose file tools*
The moose likes Java in General and the fly likes Problem parsing time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem parsing time " Watch "Problem parsing time " New topic
Author

Problem parsing time

Cesar Coz
Ranch Hand

Joined: Aug 19, 2010
Posts: 32
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 ?


Cesar Coz.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Cesar Coz
Ranch Hand

Joined: Aug 19, 2010
Posts: 32
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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Aug 11, 2007
Posts: 4659
    
    5

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

Joined: Aug 19, 2010
Posts: 32
you both are right! thanks for the advices!
 
wood burning stoves
 
subject: Problem parsing time