I have to return the string today and yesterday. The milliseconds I got from a function, so what have I got to do? I tried a lot.
if((data.getStartTimeMilli()-86400000) < data.getStartTimeMilli())
{
return "Today";
}
else
{
return "Yesterday";
}
But it will return only today.
Two millisecond values I pass in function, one is startdatemili
and enddatemili
.
It’s a range of dates.
As menthioned in the comment, usually today
does not mean 24
hours ago.
Suppose now is 2018-10-06 15:20
, 2018-10-06 00:01
should be considered as today
, while 2018-10-05 23:59
is yesterday
, though thay are both within 24
hours.
This solution works on above assumption.
Get the start millis of today:
ZonedDateTime zonedDateTime = ZonedDateTime.now();
zonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.DAYS);
long millis = zonedDateTime.toInstant().toEpochMilli();
then compare with it:
if((data.getStartTimeMilli() > millis) {
return "Today";
} else
return "Yesterday";
}
But be careful, when the start milli is before today
, does not mean it is yesterday
, it might be some day earlier.
If you do want to use 24 hours
to limit today:
Instant instant = Instant.now().minusMillis(86400000);
long millis = instant.toEpochMilli();