我有java.util.Timer類和TimerTask來調度,我想每天在凌晨2點做任務。Java延時從不運行
我的代碼:
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輸出(Task在:)運行,但永遠不會執行應該在凌晨2點運行任務,爲什麼呢?我沒有得到它我從未遇到過這樣的問題。輸出日誌中沒有錯誤。
'new Date(cooldown)'在打印語句中返回給您什麼? – nullpointer