我有一个时间戳(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
将始终早于当前时间戳(以毫秒为单位)。
最佳答案
我会说 searchTimestamp > currentTimestamp 所以你不需要绝对值,只需使用 searchTimestamp - currentTimestamp 即可。
关于java - 如何检查时间戳是否是 10 分钟前的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28865830/