0

嗨,我正在尝试编写课程以在特定时间安排工作。这是代码,

public void test(){
        Timer timer = new Timer();
        Calendar date = Calendar.getInstance();
        date.set(Calendar.HOUR,0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 2);
        date.set(Calendar.MILLISECOND,0);

        //schedule to run on 1 am every friday. 
        timer.schedule(new JobRunner(), date.getTime(),1*0*0*0);
    }

请在最后一行提供帮助,我不确定如何在其中指定时间。

谢谢

4

1 回答 1

2

我希望这有帮助。

    static final long period = 7*24*60*60*1000;//one week

public void test(){
    Timer timer = new Timer();
    Calendar date = Calendar.getInstance();
    date.set(Calendar.HOUR_OF_DAY, 1);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND,0);
    date.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
    //in case you run the batch before date
    if (date.before(Calendar.getInstance())) {
        date.add(Calendar.DAY_OF_YEAR, 7);
    }        

    long delay =  date.getTimeInMillis() - new Date().getTime();              
    //schedule to run on 1 am every friday. 
    timer.schedule(new JobRunner(), delay, period);              
}

问候!

于 2013-02-12T17:35:29.373 回答