【发布时间】:2017-09-20 12:48:26
【问题描述】:
我有 java.util.Timer 类和 TimerTask 来安排,我想每天凌晨 2 点做任务。
我的代码:
public class AutomaticPuller {
final private Timer t = new Timer();
public AutomaticPuller() {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 2);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
long cooldown = today.getTimeInMillis();
if (today.getTime().before(new Date(System.currentTimeMillis()))) {
cooldown += 24L*60L*60L*1000L;
}
System.out.println("Task will run at: " + new Date(cooldown));
TimerTask tt = new TimerTask() {
public void run() {
try {
updateAll();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.schedule(tt, cooldown, 24L*60L*60L*1000L);
}
}
我看到 println 的输出(任务将在:) 运行,但是应该在凌晨 2 点运行的任务从未执行,为什么?我不明白我从来没有遇到过这样的问题。输出日志中没有错误。
【问题讨论】:
-
new Date(cooldown)在 print 语句中返回给你什么?