Checking whether a timestamp is 10 minutes old I have a timestamp (searchTimestamp) which I need to check to see 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 &gt; 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?

getTheTimestampFromURL will always be older than the current timestamp in milliseconds.
Your description says that you want to check whether the searchTimestamp is less than 10 minutes old. Your code does something different, though.

Your code checks whether there's less than 10 minutes between the times (the difference is &lt;= 10 minutes .... If the searchTimestamp is 2 minutes in the future, it will pass the test, if it is 9 minutes in the future, it will pass the test, and if it is 11 minutes in the future, it will fail the test.

Of interest, the math you use does a 10 minute check, but the message says "greater than 5 minutes old."

Your message should say: "timestamp is older than 10 minutes, or more than 10 minutes in the future"

So, changing your code to be what I think it should be, is a lot simpler than you would think.

What you want is for the searchTimestamp to have happened sometime after 10 minutes ago.... this is the way to do it:

private static final int TEN_MINUTES = 10 * 60 * 1000;


then, in your method:

long tenAgo = System.currentTimeMillis() - TEN_MINUTES;
if (searchTimestamp &lt; tenAgo) {
    System.out.println("searchTimestamp is older than 10 minutes");
}