开发者

get date for next time at 1am

In order to run a backg开发者_开发知识库round service I want to scedule a thread for running daily at 1am. How can I get this date in java?

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, HOUR_OF_EXECUTION);
Date date = cal.getTime();

did not work.


You should go for Quartz.

  • Here is some sample code.

OR

Timer timer = new Timer();
final Callable c = callable;
TimerTask task = new TimerTask() {
    public void run() {
            c.call();
    }
}
t.scheduleAtFixedRate(task, firstExecuteDate, 86400000); // every day  

Give firstExecuteDate accordingly and your thing has done, more over to get next execute - now

To get next date for time 1 pm.

Calendar cal  = Calendar.getInstance();
Calendar calAtOne  = Calendar.getInstance();
calAtOne.set(Calendar.HOUR_OF_DAY,13);
calAtOne.set(Calendar.MINUTE,00);
calAtOne.set(Calendar.SECOND,00);

if(calAtOne.after(cal)){
    return calAtOne;
}else{
    cal.add(Calendar.DATE,1);
    cal.set(Calendar.HOUR_OF_DAY,13);
    cal.set(Calendar.MINUTE,00);
    cal.set(Calendar.SECOND,00);
    return cal;
}

Then subtract currentMillis - (nextExecDate returned By above code)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜