该方法在指定日期执行任务,若是是过去的时间,这个任务会当即被执行。
执行时间早于当前时间
示例代码,当前时间是2019年9月19日,代码中写的是前一天的时间。java
public class MyTask1 extends TimerTask { private static Timer timer = new Timer(); public void run() { System.out.println("运行了!时间为:" + new Date()); } public static void main(String[] args) throws Exception { MyTask1 task = new MyTask1(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = "2019-9-18 21:22:00"; Date dateRef = sdf.parse(dateString); System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString()); timer.schedule(task, dateRef); } }
运行结果以下code
字符串时间:2019-9-18 21:22:00 当前时间:2019-9-19 20:18:26 运行了!时间为:Thu Sep 19 20:18:26 CEST 2019
能够看到,过去的时间当即执行。
执行时间晚于当前时间
修改代码的dateString改成将来的时间,如"2019-9-19 20:22:00"
运行结果以下orm
字符串时间:2019-9-19 20:22:00 当前时间:2019-9-19 20:21:22 运行了!时间为:Thu Sep 19 20:22:00 CEST 2019
能够看到,将来的时间要等到目标时间才会执行。
多个Timer同时执行
示例代码以下字符串
public class MyTask2 extends TimerTask { private static Timer timer = new Timer(); public void run() { System.out.println("运行了!时间为:" + new Date()); } public static void main(String[] args) throws Exception { MyTask2 task1 = new MyTask2(); MyTask2 task2 = new MyTask2(); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString1 = "2019-9-19 21:12:00"; String dateString2 = "2019-9-19 21:12:00"; Date dateRef1 = sdf1.parse(dateString1); Date dateRef2 = sdf2.parse(dateString2); System.out.println("字符串时间:" + dateRef1.toLocaleString() + " 当前时间:" + new Date().toLocaleString()); System.out.println("字符串时间:" + dateRef2.toLocaleString() + " 当前时间:" + new Date().toLocaleString()); timer.schedule(task1, dateRef1); timer.schedule(task2, dateRef2); } }
运行结果以下io
字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57 字符串时间:2019-9-19 21:12:00 当前时间:2019-9-19 21:11:57 运行了!时间为:Thu Sep 19 21:12:00 CEST 2019 运行了!时间为:Thu Sep 19 21:12:00 CEST 2019
说明能够多任务执行。执行时间和当前时间的关系规则同上。class
该方法在指定的时间执行任务,间隔period时间再次执行,无限循环。
执行时间早于当前时间
示例代码date
public class MyTask3 extends TimerTask { public void run() { System.out.println("运行了!时间为:" + new Date()); } public static void main(String[] args) throws Exception { MyTask3 task = new MyTask3(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = "2019-09-19 22:01:00"; Timer timer = new Timer(); Date dateRef = sdf.parse(dateString); System.out.println("字符串时间:" + dateRef.toLocaleString() + " 当前时间:" + new Date().toLocaleString()); timer.schedule(task, dateRef, 4000); } }
运行结果以下循环
字符串时间:2019-9-19 22:01:00 当前时间:2019-9-19 22:09:13 运行了!时间为:Thu Sep 19 22:09:13 CEST 2019 运行了!时间为:Thu Sep 19 22:09:17 CEST 2019 运行了!时间为:Thu Sep 19 22:09:21 CEST 2019 运行了!时间为:Thu Sep 19 22:09:25 CEST 2019 ···
能够看到,当即执行。
执行时间晚于当前时间
运行结果以下方法
字符串时间:2019-9-19 22:12:00 当前时间:2019-9-19 22:11:24 运行了!时间为:Thu Sep 19 22:12:00 CEST 2019 运行了!时间为:Thu Sep 19 22:12:04 CEST 2019 运行了!时间为:Thu Sep 19 22:12:08 CEST 2019 运行了!时间为:Thu Sep 19 22:12:12 CEST 2019 运行了!时间为:Thu Sep 19 22:12:16 CEST 2019 ···