我有一个时间戳(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
将比当前时间戳更早,以毫秒为单位。
答案 0 :(得分:1)
我会说searchTimestamp> currentTimestamp所以你不需要abs,只需使用searchTimestamp - currentTimestamp。