• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

calendar to date and timezone

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's unclear to me what you are tying to do. What are your requirements?
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 28409
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why not just throw away the unnecessary timezone information and use only the date?
 
Bucsie Dusca
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because the xml parser creates the Calendar, not me. is there a way to transform a timezone-d Calendar into a non-timezone one?
reply
    Bookmark Topic Watch Topic
  • New Topic