aspose file tools*
The moose likes Java in General and the fly likes Output a Calendar with timezone, in W3C datetime format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Output a Calendar with timezone, in W3C datetime format?" Watch "Output a Calendar with timezone, in W3C datetime format?" New topic
Author

Output a Calendar with timezone, in W3C datetime format?

Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
I am trying to output a Calendar object (third party application requests a Calendar object in their method) with the date in W3C format
For example: 1997-07-16T19:20:30+01:00

I found a posting similar to this on this forum but it wasn't for the Calendar object. Have been stuck on this for over a day and it's driving me nuts!!

Anyone got any ideas ?

Thanks
Jon
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Jon,

I assume you've looked the Date and Calendar APIs? You'll find that there are many formatting options and even the ability to create your own custom format.


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
Yes, have looked at them over and over )
I can't use Date (or I cant see how I can) as I need to return a Calendar object and I can't see how you return the information of a Calendar object in W3C format
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

OK, the first step would be to try and format the date using SimpleDateFormat. What version of the JDK are you using?

Here's a link to the SimpleDateFormat for JDK 1.5 docs.

See if you can get the Date formatted correctly first and then we can help you tackle the step of passing it as a Calendar.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Incidentally your W3C standard format is based on the ISO8601 date format, which both Java and JodaTime (a 3rd party open source library for calendaring) support. I mention JodaTime as some people prefer it to using the standard Java classes (an as a further aside the lead dev on JodaTime is actually working on the Date/Time/Calendaring in JDK 7).

OK, I got more curious and also found this which is a Sun class for RSS Syndication feeds, it appears to have some W3C date parsing functionality (should SimpleDateFormat fall short)
[ November 14, 2008: Message edited by: Martijn Verburg ]
Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
Thanks for the help Martijn.

OK, here's some code that displays the date in the format I need it:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String text = df.format(System.currentTimeMillis());
logFile.println("date and time is " + text);
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

OK, that's a good start!

Now what have you tried so far to get that formatted String/Date into a Calendar object?
Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
Well this is where I am up against a brick wall....
I can use:

Calendar cal = Calendar.getInstance();

to create a new instance of Calendar and then....

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String text = df.format(System.currentTimeMillis());
logFile.println("date is " + text);
cal.setTime(df.parse(text));
logFile.println("Calendar is " + cal.getTime());
logFile.println("Calendar is " + cal.getInstance());

outputs the following:
date is 2008-11-14T11:58:11+0000

Calendar is Fri Nov 14 11:58:11 GMT 2008

Calendar is java.util.GregorianCalendar[time=1226663891532,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/London",offset=0,dstSavings=3600000,useDaylight=true,transitions=242,lastRule=java.util.SimpleTimeZone[id=Europe/London,offset=0,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek= 2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2008,MONTH=10,WEEK_OF_YEAR=46,WEEK_OF_MONTH=2,DAY_OF_MONTH=14,DAY_OF_YEAR=319,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=58,SECOND=11,MILLISECOND=532,ZONE_OFFSET=0,DST_OFFSET=0]
Narendira Sarma
Greenhorn

Joined: Nov 14, 2008
Posts: 18
My guess is after setting the time to the Calendar object, you can straight away pass the object to the third party tool.

(Sorry if I am wrong, but can't think of a way to set DATE-FORMAT for a calendar object!).
Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
Nope, that doesnt work
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Jon,

Sorry but I think that this isn't going to be possible, quote from the Calendar Javadoc:

"The date or time format strings are not part of the definition of a calendar, as those must be modifiable or overridable by the user at runtime. Use DateFormat to format dates."

In other words the person you are sending this to needs to get the date out of the Calendar and format it correctly. The only other thing I can think of is to get that 3 party to change their API (or add an alternative call) to allow you to pass through the formatted string.

Sorry we couldn't help more!
Jonny Kidd
Greenhorn

Joined: Feb 02, 2006
Posts: 13
Martijn - Thanks very much for that. I've contacted them and asked their thoughts on it, if I get a reply I'll let you know. I've programmed a lot of apps using their API and this is the first problem I've come across that I just can't seem to solve!
 
Consider Paul's rocket mass heater.
 
subject: Output a Calendar with timezone, in W3C datetime format?