import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
@SuppressWarnings("all")
public class TimeTaskTest {
public static void main(String[] args) throws Exception {
// 創建定時任務器
Timer timer = new Timer();
// 執行定時任務 5秒執行一次
timer.schedule(
new MTimerTask(),
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").parse("2020-4-22 00:50:00 000"),
5*1000
);
}
}
/**
* 定時任務
*/
@SuppressWarnings("all")
class MTimerTask extends TimerTask {
@Override
public void run() {
// 輸出當前執行時間
System.out.println("timertask start with:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
代碼如上所示,定時任務處理參考