java定时任务调度

package timer;  
import java.text.SimpleDateFormat;  
import java.util.Calendar;  
import java.util.Date;  
import java.util.Timer;  
  
public class Timers {  
    private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;//时间间隔(一天)   
     
     public static void timerExcute(){  
               Timer timer = new Timer(); //定时器  
               WeatherTimer weatherTimer=new WeatherTimer();//执行的任务  
               Calendar calendar = Calendar.getInstance();   
             calendar.set(Calendar.HOUR_OF_DAY, 1); //凌晨1点   
             calendar.set(Calendar.MINUTE, 0);   
             calendar.set(Calendar.SECOND, 0);   
             Date date=calendar.getTime(); //第一次执行定时任务的时间   
             //如果第一次执行定时任务的时间 小于当前的时间   
             //此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。   
             if (date.before(new Date())) {   
                 date = Timers.addDay(date, 1);   
             }    
             System.out.println("data is = "+new SimpleDateFormat("yyyy-MM:dd HH:mm:ss").format(date));  
             timer.schedule(weatherTimer, date, PERIOD_DAY);//每天12点执行当前任务  
  }   
    // 增加或减少天数   
    public static Date addDay(Date date, int num) {   
        Calendar startDT = Calendar.getInstance();   
        startDT.setTime(date);   
        startDT.add(Calendar.DAY_OF_MONTH, num);   
        return startDT.getTime();   
    }   
      
  
/*** 
  *需集成当前时间任务类 
  */  
  public class WeatherTimer  extends Timer{  
           
          public void run(){  
                     
         }  
      
}  
  
  
     public static void main(String[] args) {  
          Timers.timerExcute();  
     }  
} 

 

posted @ 2018-01-25 10:32  light-zhang  阅读(298)  评论(0编辑  收藏  举报