public class TimerTest { //第一种方法:设定指定任务task在指定时间time后执行 //schedule(timertask,Date time) public static void timer1(){ Timer timer = new Timer(); timer.schedule(new TimerTask(){ public void run(){ System.out.println("-------timer1 test -----"); } }, 5000); } //第二种方法:设定指定任务task在指定延迟delay后进行固定延迟period周期的进行 //schedule(TimerTask task, long delay, long period) public static void timer2(){ Timer timer = new Timer(); timer.schedule(new TimerTask(){ public void run(){ System.out.println("-------timer2 test -----"); } }, 1000,5000); } //第三种方法第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。 //scheduleAtFixedRate(TimerTask task, long delay, long period) public static void timer3(){ Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask(){ public void run(){ System.out.println("-------timer3 test -----"); } }, 1000,5000); } //第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行. // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period) public static void timer4(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR, 14); calendar.set(Calendar.MINUTE,30); calendar.set(Calendar.SECOND,00); Date time = calendar.getTime(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask(){ public void run(){ System.out.println("-------timer4 test -----"); } },time,1000*60*60*24); } public static void main(String []args){ timer1(); timer4(); } }