This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Get current date at midnight? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get current date at midnight?" Watch "Get current date at midnight?" New topic

Get current date at midnight?

Kellin Arbuckle

Joined: Jul 06, 2004
Posts: 1
How can I set a variable equal to the current date with the time set to midnight?

In other words, each time my program runs, I want:

myDate = MM/dd/yyyy 00:00:00

Where MM/dd/yyyy is the current date.
Jeremy Wilson
Ranch Hand

Joined: Feb 18, 2003
Posts: 166
Create an object of gregorian calender. You can create a date object using individual ints for the different month, day, ....

Jeremy Wilson
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi Kent,

Jeremy had already given you the clue to get started.

Since you need the current date, your best bet would be the system date, that will always give you today's (current) date.

Using the java.util.GregorianCalendar class, you're able to get this current date. Hint: look at its constructor.

Your second part involves setting your time components, namely HOUR, MINUTE & SECOND into 0s. So there must be some way (methods) that can help you to do the setting jobs.

The API for GregorianCalendar will be able to help you get all the pieces together into your desired codes.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
I agree. Here's the link:
subject: Get current date at midnight?
It's not a secret anymore!