I need to check if given date is after today 23:59:59, how can I create date object that is today 23:59:59?
5 Answers
Use java.util.Calendar:
Calendar cal = Calendar.getInstance(); // represents right now, i.e. today's date
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 999); // credit to f1sh
Date date = cal.getTime();
I think you might be approaching this from slightly the wrong angle, though. Instead of trying to create a Date instance that's one atom before midnight, a better approach might be to create the Date that represents midnight and testing whether the current time is strictly less than it. I believe this would be slightly clearer in terms of your intentions to someone else reading the code too.
Alternatively, you could use a third-party Date API that knows how to convert back to date. Java's built-in date API is generally considered to be deficient in many ways. I wouldn't recommend using another library just to do this, but if you have to do lots of date manipulation and/or are already using a library like Joda Time you could express this concept more simply. For example, Joda Time has a DateMidnight
class that allows much easier comparison against "raw" dates of the type you're doing, without the possibility for subtle problems (like not setting the milliseconds in my first cut).

- 102,507
- 33
- 189
- 228
-
1Regarding `Calendar.getInstance()`: Keep in mind that the returned `Calendar` also contains the current value for milliseconds. I didn't know that and recently it took me a whole day to find out why `equals()` always returned false. – f1sh Aug 04 '10 at 11:05
-
Good point f1sh - perhaps the approach here is slightly wrong, see upcoming edit. – Andrzej Doyle Aug 04 '10 at 11:06
-
-
@Adrian, it's *probably* 999; though that's what I meant by the approach being wrong, and suggesting `< 00:00:00` instead of `<= 23:59:99`. – Andrzej Doyle Aug 04 '10 at 11:11
This creates a date in the future and compares it with the current date (set to late evening). You should consider using the Joda Time Library.
long timeStampOfTomorrow = new Date().getTime() + 86400000L;
Date dateToCheck = new Date(timeStampOfTomorrow);
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 23);
today.set(Calendar.MINUTE, 59);
today.set(Calendar.SECOND, 59);
today.set(Calendar.MILLISECOND, 999);
boolean isExpired = dateToCheck.after(today.getTime());
With Joda Time Library you could do this more readable. An easy example can be found on the project website.
public boolean isRentalOverdue(DateTime datetimeRented) {
Period rentalPeriod = new Period().withDays(2).withHours(12);
return datetimeRented.plus(rentalPeriod).isBeforeNow();
}

- 11,181
- 18
- 74
- 102
Use the Date.before(Date) or Date.after(Date)
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MINUTE, 59);
Date d = c.getTime()
Date x = // other date from somewhere
x.after(d);

- 24,302
- 4
- 42
- 43
Find the tomorrow date convert it into millisecond you get a long value. subtract -1 from it and again convert to date you will get your required date.
ex : 12:00:00 of tomorrow convert it into milliseconds. you will get like a long value 312313564774 subtract -1 you will get today eod time in milliseconds

- 11,606
- 10
- 35
- 40
I assume you meant “is the given java.util.Date after today?”.
If so, do not fret about determining the last moment of the day; that is impracticable because of the fractional second. Better to focus on the date only, without the time-of-day. You want to know if the date-only of the given moment is after the date-only of the current moment. A time zone is crucial here.
tl;dr
ZoneId z = ZoneId.of( “America/Montreal” ); // Time zone determines date.
Boolean givenMomentIsAfterToday = myUtilDate.toInstant().atZone( z ).toLocalDate().isAfter( LocalDate.now( z ) );
Details
You are using troublesome old date-time classes now supplanted by the java.time classes.
First, convert the given java.util.Date
to an Instant
. Find new conversion methods added to the old classes for converting to/from java.time types.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = myUtilDate.toInstant();
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
ZoneId z = ZoneId.of( “America/Montreal” );
LocalDate today = LocalDate.now( z );
ZonedDateTime
Adjust the instant into the same time zone for comparison of dates.
ZonedDateTime zdt = instant.atZone( z );
Interrogate for the date-only value, a LocalDate
.
LocalDate ldt = zdt.toLocalDate();
Lastly, compare the LocalDate
objects.
Boolean givenMomentIsAfterToday = ldt.isAfter( today );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.

- 1
- 1

- 303,325
- 100
- 852
- 1,154