Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes calendar to date and timezone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "calendar to date and timezone" Watch "calendar to date and timezone" New topic
Author

calendar to date and timezone

Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
Hi
I retrieve Calendar objects from a file, and my Calendar object reads the date: 2006-04-05+03:00

if I do a calendar.getTime() to convert the Calendar to a Date, the Date will be: Tue Apr 04 22:00:00 BST 2006 - probably because my timezone is not +03, it's +0.

so instead of having April 5th, I get April 4th.

This is obviously wrong. I found a solution to set the date like this:



and this returns Wed Apr 05 01:00:00 BST 2006

so... still not April 5th 00:00, but the hour is not relevant, only the day is.

My question is: is there a better, maybe a more elegant way to "transform" the Calendar into a Date correctly? A way to ignore the +03:00 part of the Calendar?

Thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
TimeZone is not part of the state of Date object. Think of a Date object
as just a timestamp -- the number of milliseconds since midnight, 1 Jan 1970 GMT.
Unfortunately, Date's toString method formats itself using your JVM default TimeZone.
(I'd rather have it's toString method return this raw long value.)

Anyway, I think the solution to do your formatting explicitly, using DateFormat:


There is no emoticon for what I am feeling!
Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
that is interesting, but I need the code to be generic, so i don't know what timezone will be where/when the program is ran.
The problem is that Calendar has the method getTimeInMillis() - which should be the timestamp, why else is it there?
so a call like new Date(calendar.getTimeInMillis()) should print the same date, but it doesn't.
otherwise, there should be getTimeInMillis and getTimeInMillisNoTimeZone or getTimeZoneMillis.

using DateFormat to "translate" a Calendar to a Date is just as un-elegant as
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
It's unclear to me what you are tying to do. What are your requirements?
Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
What I am doing is:

An xml parser retrieves for me a Calendar object by reading a file (the file was created in another timezone).
I need to pass this time to a constructor of another class but as a Date.
The problem is that in debug I see that the Calendar (actually XmlCalendar) instance is 2006-04-05T00:00:00.000+03:00 - so it is correct
and when I do a new Date(calendar.getTimeInMillis()) in debug the date is: Tue Apr 04 21:00:00 GMT 2006 - but normally it should be the same date, april 5th,00:00

I can only retrieve the correct time as a Date by doing
new Date(calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET))

is this really the way to get the correct Date?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Here's the thing:

Calendar has a time zone, Date does not.

If you "convert" a calendar to a Date, time zone information is lost. Is that what you want to do?
Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
yes that's it.
it just strikes me as odd that one cannot convert a Calendar to a Date in a single instruction. doesn't anybody need to do this?
(oh and thank you for the replies )
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You write that your Calendar is 2006-04-05T00:00:00.000+03:00. With all those zeroes am I right in assuming this element/attribute in your XML file is intended to specify a year, month and day, but no more information? That it's no indended to specify hour, minute, second, milliseconds or time zone? And how is the field formatted in the original XML file? Is it formatted with time zone information? If the answer is yes to both these questions, it sounds like the XML file is misformatted. It should contain a field like "2006-04-05" only.
Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
yes, this could be it, the XML contains:
2006-04-05+03:00
the hour,minutes etc are not important, it is just the date that counts
[ April 04, 2006: Message edited by: Diana Pop ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Then why not just throw away the unnecessary timezone information and use only the date?
Bucsie Dusca
Ranch Hand

Joined: Oct 18, 2004
Posts: 31
because the xml parser creates the Calendar, not me. is there a way to transform a timezone-d Calendar into a non-timezone one?
 
 
subject: calendar to date and timezone