【发布时间】:2017-08-27 19:36:04
【问题描述】:
我正在尝试确定时间戳是否早于 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;
}
【问题讨论】:
-
请发帖Minimal, Complete, and Verifiable example。您的代码不是那样,例如什么是
target,你怎么称呼cooldown()? -
所有这些代码都是多余的。唯一的问题是为什么时间戳比原来更早。
-
如果其余代码非常冗余,那么创建一个最小示例应该很容易,对吧?
-
刚刚在链接中查看了您在 Ideone 上的代码。在该代码中,不会创建您的类
Ideone的实例。所以secondsAgo永远不会设置为 30。@Andreas 的怀疑是正确的。 -
当我问你是否确定
secondsAgo的值是30,你说是的,甚至没有检查。我问,因为这是我能看到的唯一原因,它会显示结果,所以我相当肯定它是0,事实证明,我是对的。下一次,验证,例如使用调试器。如果您不知道如何调试,现在是学习的好时机。是程序员必备的技能。见What is a debugger and how can it help me diagnose problems?
标签: java timestamp unix-timestamp