我用的是java.util包下的timer类,想实现每天24点执行一段代码,应该怎么设参数啊?
Timer timer = new Timer(true);
timer.schedule(task, firstTime, period)
有好几个schedule方法,我应该用哪个?参数怎么写呢?
 

解决方案 »

  1.   

    定时器public class TimerListener extends HttpServlet implements ServletContextListener {
    private static final long serialVersionUID = 1L;
    private TimerManager tm = null;   public void contextInitialized(ServletContextEvent sce) {
          int day,hour,min,sec;      
          day = 1; hour = 23; min = 59; sec = 59;     
          tm = new TimerManager(day,hour,min,sec);
        }    public void contextDestroyed(ServletContextEvent sce) {
           tm.destoryTimer();
        }
    }public class TimerManager {
        Timer t = null;
        public TimerManager(int day,int hour,int min,int sec) {
            time(day,hour,min,sec);
        }
     
        public void time(int day,int hour,int min,int sec){    
        Calendar c = Calendar.getInstance();
        //c.set(Calendar.DAY_OF_MONTH,day);
        c.set(Calendar.HOUR_OF_DAY,hour);
        c.set(Calendar.MINUTE,min);
        c.set(Calendar.SECOND,sec);
        Date date = c.getTime();
        
        t= new Timer();
        MyTask mt = new MyTask();
        t.schedule(mt,date,24*60*60*1000);//你这样写就可以了
        
        }
        
        public void destoryTimer(){
         t.cancel();
        }}public class MyTask  extends TimerTask{
        public MyTask() {}
        
        public void run() {
            //这里写你要执行的代码
    System.out.println("这里写你要执行的代码...");
        }}
      

  2.   

    day = 1; hour = 23; min = 59; sec = 59; //这是设置时间的,测试时你可以改成
    day = 1; hour = 14; min = 0; sec = 0;
      

  3.   

    计划任务 + Runnable jar
      

  4.   

    如果你需要 24点准时执行一段代码不应该使用:
    timer.schedule(task, firstTime, period)
    而应该使用:
    timer.scheduleAtFixedRate(task, firstTime, period)
    它们之间是有区别的。
      

  5.   

    scheduleAtFixedRate的使用方法 给你个小demo  自己可以在本机上运行,瞅瞅
    /**
     * @author troy(J2EE)
     * @version 1.0
     */
    import java.util.*;
    import java.text.*;
    public class DoTask {   
        private final static Timer timer = new Timer();
    private static Date firstTime;//任务首次执行时间
    private final static long PERIO_TIME=2 * 1000;//任务运行周期: 单位毫秒
    public final static Timer getTimer(){
    return timer;//获取timer对象的实例
    }
        public void start() throws Exception{
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           firstTime=sdf.parse("2010-07-26 16:06:10");
           timer.scheduleAtFixedRate(new SendMsgTask(),firstTime,PERIO_TIME);//设置定时任务
        } 
    public static void main(String[] args) throws Exception{ 
           new DoTask().start(); //开启任务
        }
    }
    class SendMsgTask extends TimerTask{
    private static int i=0;
    public void run() { 
    sendMsg();

    private void sendMsg() {
    i++;
    System.out.println("开始执行-- 开始 (第"+i+"次)");
    if(i==5){
    DoTask.getTimer().cancel();//当执行任务5次后停止--计时器
    }

    }
      

  6.   

    任务调度的代码不是这样写的。一般会记录下一次执行的时间,等到当前时间大于或等于下一次执行时间时就执行。一个功能完善且安全的任务调度工具实现是非常困难的,不建议自己写,用现成的 Quartz 吧。如果想了解一下的话,可以看一下这个帖子我在 23 楼的回复:http://topic.csdn.net/u/20100527/22/45a95f02-3ffd-4430-92ec-a9b62503a15e.html
      

  7.   

    http://topic.csdn.net/u/20100705/18/73d51340-631f-4b9a-89e7-3125df30fec9.html?seed=351074017&r=66736573#r_66736573
    参见帖子.  执行时间自己设定, 相当于闹钟.
      

  8.   

    quartz中cronExpression配置说明| 关注Java
      

  9.   

    http://topic.csdn.net/u/20100527/22/45a95f02-3ffd-4430-92ec-a9b62503a15e.html
    23楼相当牛
      

  10.   

        是的   我定时器都是用quartz来做的啊  不过定时器有时候也会不跑  这种情况是比较少,但还是有的 
      所以  我建议LZ 定时器定时跑,如果没跑的话,你可以在后台管理加个链接手动跑下这段代码啊
      

  11.   

    public static void showTimer() {
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    
                    System.out.println("执行定时任务!!!");
                }         };         Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            System.out.println(day);
            /*** 时间自己修改 ***/
            calendar.set(year, month, day, 20, 34, 00);
            Date date = calendar.getTime();
            Timer timer = new Timer();
            timer.schedule(task, date,1000);
        }     public static void main(String[] args) {
            showTimer();
        }
      

  12.   

    也可以去网上搜一下关于quartz的资料;
      

  13.   


    这种Daily Job、Nightly Builds,个人还是喜欢用OS的任务
      

  14.   

    特别是那个帖子,每个月一次的任务,更没必要用java来定时