Search code examples
javatimestampmilliseconds

How to check whether a timestamp is 10 minutes old?


I have a timestamp (searchTimestamp) which I need to check whether it is less than 10 minutes old or not.

long currentTimestamp = System.currentTimeMillis();

long searchTimestamp = getTheTimestampFromURL();// this also gives me back timestamp in 13 digit (1425506040493)

long difference = Math.abs(currentTimestamp - searchTimestamp);

System.out.println(difference);

if (difference > 10 * 60 * 1000) {
    System.out.println("timestamp is greater than 5 minutes old");
}

I have got the above code which is working fine. Is this the right way to do this or is there any better way?

NOTE:

getTheTimestampFromURL will be older than the current timestamp in milliseconds always.


Solution

  • I'd say that searchTimestamp > currentTimestamp so you don't need an abs and just use searchTimestamp - currentTimestamp.