• 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
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Woa! Why does Date/Calendar do this?

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two Date objects. One holds just the date, and one holds just the time. You would THINK that adding the getTime of both of them would give you the date and time but it doesn't give you what you think!

Try this sample program:



You would think that you would get today's date with 1630 (4:30 pm) as the time...but you get 22:30. I think this has something to do with GMT, but given that none of the Date or Calendar objects show that (when you print them out they show you CST or CDT where I am).

Why does it add four hours like that?

Also, what is the most efficient way to get it corrected (to show 1630 instead of 2230)? Would it be just to add what I get from GregorianCalendar.get(Calendar.ZONE_OFFSET) to the date?

For example:

GregorianCalendar cal2 = new GregorianCalendar();
int offset = cal.get(Calendar.ZONE_OFFSET);
cal2.setTimeInMillis(date.getTime() + time.getTime() + offset);

Thanks!
[ October 05, 2006: Message edited by: Darrin Smith ]
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


getTime
public long getTime()Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

Returns:
the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.



using get time returns the number of milliseconds since the epoch in GMT.
So your code returns 21:30 in my EST timezone.

How bout something like this?
 
Darrin Smith
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That works too as does using the Calendar.get(Calendar.ZONE_OFFSET. I think the Calendar.get might be the more efficient of the two so I'll probably stick to that.

Anyway, thanks for the reply. It may come in handy!
[ October 05, 2006: Message edited by: Darrin Smith ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look into the TimeAndMoney and Joda Time packages. Java dates are pretty painful some times.
 
This tiny ad is suggesting that maybe she should go play in traffic.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic