Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting the time

 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 269
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local time or GMT ?
W.
 
Marcel de Jong
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
this should do the trick.

Regards,
Marcel
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should HOUR_OF_DAY be 4?
W.
 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, HOUR_OF DAY should be 4 for 4am. Thanks Marcel.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Duran
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic