In my code I need to find all my things that happened today. So I need to compare against dates from today at 00:00am (midnight early this morning) to 12:00pm (midnight tonight).
I know ...
Date today = new Date();
... gets me right now. And ...
Date beginning = new Date(0);
... gets me zero time on Jan 1, 1970. But what's an easy way to get zero time today and zero time tomorrow?
UPDATE; I did this, but surely there's an easier way?
Calendar calStart = new GregorianCalendar();
calStart.setTime(new Date());
calStart.set(Calendar.HOUR_OF_DAY, 0);
calStart.set(Calendar.MINUTE, 0);
calStart.set(Calendar.SECOND, 0);
calStart.set(Calendar.MILLISECOND, 0);
Date midnightYesterday = calStart.getTime();
Calendar calEnd = new GregorianCalendar();
calEnd.setTime(new Date());
calEnd.set(Calendar.DAY_OF_YEAR, calEnd.get(Calendar.DAY_OF_YEAR)+1);
calEnd.set(Calendar.HOUR_OF_DAY, 0);
calEnd.set(Calendar.MINUTE, 0);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
Date midnightTonight = calEnd.getTime();
Other answers are correct, especially the java.time answer by arganzheng. As some mentioned, you should avoid the old java.util.Date/.Calendar classes as they are poorly designed, confusing, and troublesome. They have been supplanted by the java.time classes.
Let me add notes about strategy around handling midnight and spans of time.
Half-Open
In date-time work, spans of time are often defined using the “Half-Open” approach. In this approach the beginning is inclusive while the ending is exclusive. This solves problems and if used consistently makes reasoning about your date-time handling much easier.
One problem solved is defining the end of the day. Is the last moment of the day
23:59:59.999
(milliseconds)? Perhaps, in the java.util.Date class (from earliest Java; troublesome – avoid this class!) and in the highly successful Joda-Time library. But in other software, such as database like Postgres, the last moment will be23:59:59.999999
(microseconds). But in other software such as the java.time framework (built into Java 8 and later, successor to Joda-Time) and in some database such the H2 Database, the last moment might be23:59.59.999999999
(nanoseconds). Rather than splitting hairs, think in terms of first moment only, not last moment.In Half-Open, a day runs from the first moment of one day and goes up to but does not include the first moment of the following day. So rather than think like this:
…think like this…
In database work, this approach means not using the
BETWEEN
operator in SQL.Start of day
Furthermore, the first moment of the day is not always the time-of-day
00:00:00.0
. Daylight Saving Time (DST) in some time zones, and possibly other anomalies, can mean a different time starts the day.So let the java.time classes do the work of determining the start of a day with a call to
LocalDate::atStartOfDay( ZoneId )
. So we have to detour throughLocalDate
and back toZonedDateTime
as you can see in this example code.Note the passing of the optional
ZoneId
. If omitted your JVM’s current default time zone is applied implicitly. Better to be explicit.Time zone is crucial to date-time work. The Question and some other Answers are potentially flawed because the do not consciously handle time zone.
Convert
If you must use a java.util.Date or .Calendar, look for new conversion methods added to those old classes.
Span of time
By the way, if you are doing much work with spans of time take a look at:
Duration
Period
Interval
The
Interval
class is found in the ThreeTen-Extra project, an extension to the java.time framework. This project is the proving ground for possible future additions to java.time.Interval todayMontreal = Interval.of( todayStart.toInstant() , tomorrowStart.toInstant() );
For sake of completeness, if you are using Java 8 you can also use the
truncatedTo
method of theInstant
class to get midnight in UTC.As written in the Javadoc
Hope it helps.
The easiest way to find a midnight:
Next day:
java.time
If you are using Java 8 and later, you can try the java.time package (Tutorial):
Pretty much as the answers before, but nobody mentioned AM_PM parameter:
Because of one day is
24 * 60 * 60 * 1000
ms, the midnight of this day can be calculate as...