# 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 > 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 <= 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;


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


joda-time is a very nice library that can overcome some of the shortfalls in the core Java Date/Time classes. For example:

DateTime now = new DateTime();

DateTime fromString = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(input);
DateTime fromDate = new DateTime(timeInMillis);
DateTime then = fromDate;

Interval interval = new Interval(then, now);
if (interval.toDuration().getStandardMinutes() > 10) {
...
}


I wouldn't use Math.abs(...) to bypass not knowing which way around to compare your timestamps. That's just lazy.

Also, 10, 60, etc. are magic numbers relating to time. joda-time has DateTimeConstants.MILLIS_PER_MINUTE if you must have the number, but for manipulating DateTimes you should be using functions rather than directly manipulating the millis. Core Java, you can use TimeUnit.MINUTES.toMillis(10).

• Why should one use a library for something that can be done native? Commented Jan 31, 2019 at 14:34

To check if date is today, use Android utils library

DateUtils.isToday(long timeInMilliseconds)

This utils class also offers human readable strings for relative times. For example,

DateUtils.getRelativeTimeSpanString(long timeInMilliseconds) -> "42 minutes ago"

The are several parameters you can play with to define how precise the time span should be

• Welcome on Code Review. The OP never talk about Android, so, I'm not sure your response could be helpful. (especially considering the posting date) Commented Nov 20, 2018 at 10:34