# Checking if future time (epoch milliseconds) is within 3 hours of current time (epoch milliseconds)

I am trying to compare two times to see if a future time is within the next 3 hours of current time.

Below is some code that I have tried where I tried taking the future time minus 10,800,000 and if the current time is greater than or equal to the difference it should return true. I do not know if 10,800,000 is the correct thing to use though since sometimes it is returning true/false when it should now.

``````  public static Boolean checkIfWithin3Hours(long startTime, long currentTime){
if(currentTime >= startTime - (10800000)) {
return true;
}
return false;
}
``````

I would expect it to return true if the epoch time in milliseconds is within 3 hours of the future time in ms.

0 Followers

Most reacted comment
1 Comment authors
Recent comment authors
Subscribe
Notify of
Guest

tl;dr Your Question is confusing, not clear on exact details of your business logic. But this should get you pointed in the right direction. Instant .ofEpochSecond( … ) .minus( Duration .ofHours( 3 ) ) .isBefore( Instant.now() ) java.time We have classes for this. Do not roll-your-own date-time solution. Instant Get the current moment in UTC. Instant now = Instant.now() ; Duration Specify your wait time as a Duration, a span of time unattached to the timeline. Duration d = Duration.ofHours( 3 ) ; Specify your start time. You did not explain, but apparently the starting moment was given to you… Read more »

Guest
``````1000 milliseconds/second
60 seconds/minute
60 minutes/hour
3 hours
``````

Multiply it all together and you get 10,800,000 milliseconds. First step, your number is correct.

Next step, calculate whether the number is in the range:

If timeTesting is in the range of 3 hours from now, timeNow <= timeTesting <= timeNow + 10,800,000 ms.

Second step, your range test is incorrect.

Fix it with the following:

``````public static Boolean checkIfWithin3Hours(long startTime, long currentTime){
return startTime <= currentTime && currentTime <= startTime + 10800000;
}
``````