我正在尝试确定时间戳是否超过30秒,但由于某种原因,它的返回时间超过了30秒,甚至还没有过几秒钟。检查时间戳是否超过30秒
public static Boolean cooldown(int id) {
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, -secondsAgo);
long timeAgo = now.getTimeInMillis();
if (cooldown.containsKey(id)) {
System.out.println(cooldown.get(id) + " | " + timeAgo);
// Stored timestamp always older than timeAgo
if (cooldown.get(id) < timeAgo) {
cooldown.remove(id);
} else {
// This code should be executed, as I am running the function one after another from same UUID not even a second or two apart.
return false;
}
}
now = Calendar.getInstance();
cooldown.put(id, now.getTimeInMillis());
return true;
}
请发布[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。你的代码并非如此什么是“目标”,你如何称呼“冷却时间()”? – Andreas
所有的代码真的是多余的。唯一的问题是为什么时间戳会比它更早。 – JordanSasquatchMan
如果代码的其余部分非常冗余,那么创建一个最简单的例子应该很简单,对吧? –