import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
public static void main(String[] args) throws Exception {
// 创建定时器对象
Timer timer = new Timer();
// 指定定时任务
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Date firstTime = sdf.parse("2021-12-18 09:50:00");
timer.schedule(new LogTimerTask(), firstTime, 1000 * 10);
}
}
// 定时任务类
// 记录日志的定时任务
class LogTimerTask extends TimerTask {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
String strTime = sdf.format(new Date());
System.out.println(strTime + "完成了一次数据备份。");
}
}
javase:定时器
最新推荐文章于 2024-08-15 14:47:41 发布