Given a time in milliseconds, how can you check if it was yesterday?
-
323:39 is not millis! – uneq95 Feb 17 '19 at 00:40
-
In which time zone? It’s never the same date in all time zones. – Ole V.V. Feb 17 '19 at 04:47
-
Possible near-duplicate of [How to check if a date Object equals yesterday?](https://stackoverflow.com/questions/3006150/how-to-check-if-a-date-object-equals-yesterday) – Ole V.V. Feb 17 '19 at 04:55
-
Your Question is not clear. Rewrite with more explanation, perhaps with some examples. – Basil Bourque Feb 17 '19 at 05:30
3 Answers
You would first convert the millis to a Date
or LocalDate
and then run the comparison.
Here's a quick example:
import java.time.*;
class DateCheckSample {
public static void main(String[] args) {
// Our input date
long millis = System.currentTimeMillis();
// Convert the millis to a LocalDate
Instant instant = Instant.ofEpochMilli(millis);
LocalDate inputDate = instant.atZone(ZoneId.systemDefault()).toLocalDate();
// Grab today's date
LocalDate todaysDate = LocalDate.now();
System.out.println(millis);
// Check if date is yesterday
if (todaysDate.minusDays(1).equals(inputDate)) {
System.out.println(inputDate + " was yesterday!");
} else {
System.out.println(inputDate + " was NOT yeseterday!");
}
}
}
The Result:
2019-02-16 was NOT yesterday!
If you'd like to confirm it's working, just subtract 100000000
from millis
before running.
Side Note: As pointed out in the comments on your question,
23:59
is not a millis value...

- 9,885
- 4
- 28
- 63
-
-
1@Dr.Bright - I am confused by your comment. Are you saying that the output is wrong? It is 2019-02-16 in your time zone right now? – Zephyr Feb 17 '19 at 01:18
-
1Good answer, except for that call to `toLocalDateTime`. Unnecessary. And illogical as converting to `LocalDateTime` discards all time zone infirmation. – Basil Bourque Feb 17 '19 at 04:04
-
I suggest breaking up your code to multiple lines, to make the `ZonedDateTime` obvious. – Basil Bourque Feb 17 '19 at 04:05
If you don't want to use Date
, you can simply use the modulus operator with a bit of clever arithmetics. System#currentTimeMillis
returns the amount of milliseconds that have passed since January 1, 1970, midnight (00:00).
Combining this with the number of milliseconds in a day (86,400,000), we can figure the time at which the last start of day was — which is when today began. Then we can see if the time given to us is smaller or larger than that value.
boolean isToday(long milliseconds) {
long now = System.currentTimeMillis();
long todayStart = now - (now % 86400000);
if(milliseconds >= todayStart) {
return true;
}
return false;
}
To check if a time is yesterday instead of today, we simply check if it is between the start of today and the start of yesterday.
boolean isYesterday(long milliseconds) {
long now = System.currentTimeMillis();
long todayStart = now - (now % 86400000);
long yesterdayStart = todayStart - 86400000;
if(milliseconds >= yesterdayStart && < todayStart) {
return true;
}
return false;
}
-
But he wants to check if its yesterday :P Its clever. We can check that too. – uneq95 Feb 17 '19 at 00:57
-
You can convert the milliseconds to Date
and then compare the day with today's Date
.
For reference: Convert millisecond String to Date in Java

- 2,158
- 2
- 17
- 28
-
But i want: 8:25 PM (today) minus 8:26h is yesterday, how to check this? – Dr. Bright Feb 17 '19 at 00:51
-
You need to have the date as well with you. Otherwise you cant check just by comparing the times – uneq95 Feb 17 '19 at 00:53
-
1The terrible `Date` class was supplanted years ago by `java.time.Instant`. No need to ever use `Date` again. – Basil Bourque Feb 17 '19 at 04:07
-