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
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
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.
Assuming that your timestamp was created via System.currentTimeMillis()
(or any other compatible mechanism), you can do the following:
Create a Calendar
instances and set the hour, minute, second and millisecond fields to zero. This is today at 0:00:00,0.
Clone the instance and add 1 day. You'll get tomorrow at 0:00:00,0.
Now check if your timestamp is in the range between today.getTime()
(inclusive) and tomorrow.getTime()
(exclusive).