如何检查时间戳是否为10分钟?

时间:2015-03-04 21:58:28

标签: java timestamp milliseconds

我有一个时间戳(searchTimestamp),我需要检查它是否不到10分钟。

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");
}

我有上面的代码,工作正常。这是正确的方法吗?或者有更好的方法吗?

注意:

getTheTimestampFromURL将比当前时间戳更早,以毫秒为单位。

1 个答案:

答案 0 :(得分:1)

我会说searchTimestamp> currentTimestamp所以你不需要abs,只需使用searchTimestamp - currentTimestamp。