Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the timestamp is current date's timestamp [duplicate]

I have a scheduler that needs to check if the incoming timestamp is current day's timestamp.

The incoming timestamp will be of the format Eg:1384956395.

How to check this in java? Please help. I am not using Joda

like image 488
Poppy Avatar asked Feb 14 '23 15:02

Poppy


1 Answers

The epoch you posted is in seconds. Java uses milliseconds so you have to convert it and then compare the two.

long epochInMillis = epoch * 1000;
Calendar now = Calendar.getInstance();
Calendar timeToCheck = Calendar.getInstance();
timeToCheck.setTimeInMillis(epochInMillis);

if(now.get(Calendar.YEAR) == timeToCheck.get(Calendar.YEAR)) {
    if(now.get(Calendar.DAY_OF_YEAR) == timeToCheck.get(Calendar.DAY_OF_YEAR)) {
    }
}

You can also change the time zone if you do not want to use the default, in case the input epoch is in a different time zone.

like image 110
Dodd10x Avatar answered Feb 17 '23 11:02

Dodd10x