NOTE
This answer is most likely no longer accurate for Java 8 and beyond, because there is a better date/calendar API now.
The standard alternate is using the Calendar
Object.
Calendar cal = Calendar.getInstance(); // that is NOW for the timezone configured on the computer.
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Date date = cal.getTime();
Calendar
has the advantage to come without additional libraries and is widely understood. It is also the documented alternative from the Javadoc of Date
The documentation of Calendar
can be found here: Javadoc
Calendar
has one dangerous point (for the unwary) and that is the after
/ before
methods. They take an Object
but will only handle Calendar
Objects correctly. Be sure to read the Javadoc for these methods closely before using them.
You can transform Calendar
Objects in quite some way like:
- add a day (
cal.add(Calendar.DAY_OF_YEAR, 1);
) - "scroll" through the week (
cal.roll(Calendar.DAY_OF_WEEK, 1);
) - etc
Have a read of the class description in the Javadoc to get the full picture.