Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Output a Calendar with timezone, in W3C datetime format?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jonny Kidd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, that doesnt work
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
reply
    Bookmark Topic Watch Topic
  • New Topic