wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Setting the time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting the time" Watch "Setting the time" New topic
Author

Setting the time

David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
This seems like it should be easy but my brain is farting, it's Friday, and I've been confused by all the date/time objects Java has...
What's the easiest way to create a "time" object, whether Date or GregorianCalendar (whichever is easiest), and set it to 04:00:00 exactly, ie. 4am?
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Local time or GMT ?
W.
Marcel de Jong
Ranch Hand

Joined: May 27, 2002
Posts: 54
David,
this should do the trick.

Regards,
Marcel
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Should HOUR_OF_DAY be 4?
W.
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Yes, HOUR_OF DAY should be 4 for 4am. Thanks Marcel.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Note that you probably want to call calendar.getTime() afterwards to extract a lightweight Date object representing the time, allowing you to re-use the heavyweight Calendar to create other Dates. And you should be aware that even though it wasn't mentioned, the Calendar (and Date(s)) have year, month, and day information as well - all set to today's date (at time of Calendar creation) unless you change them. This could lead to problems if you are careless - 4 AM using a Calendar created just before midnight is not the same as 4 AM using a Calendar created just after midnight. You may want to manually set the date to some arbitrary constant value whenever you create a Calendar, so this is not an issue.


"I'm not back." - Bill Harding, Twister
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Jim Yingst:
Note that you probably want to call calendar.getTime() afterwards to extract a lightweight Date object representing the time, allowing you to re-use the heavyweight Calendar to create other Dates....

How do you know that Calendar is a heavy weight object and Date is a light weight object? That would be interesting to know about several Classes.


GenRocket - Experts at Building Test Data
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Jim- Thanks for the heads up. To take care of that I get an instance of a calendar, extract the current date from it, set the calendar to 4am, then compare if the current date is before or after 4am on that day and adjust if necessary.

[ October 02, 2002: Message edited by: David Duran ]
 
Consider Paul's rocket mass heater.
 
subject: Setting the time
 
Similar Threads
roll doesn't work properly
java.util.Date does it contain information about hours?
unable to retrieve timestamp in my servlet
unable to retrieve timestamp in my servlet
How can I get the midnight GMT time