使用下面这个定时任务方法实现
ScheduledFuture<?> java.util.concurrent.ScheduledExecutorService.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
Parameters : command the task to executeinitialDelay the time to delay first executionperiod the period between successive executionsunit the time unit of the initialDelay and period parameters
Returns : a ScheduledFuture representing pending completion ofthe task, and whose get() method will throw anexception upon cancellation
Throws : RejectedExecutionException - if the task cannot bescheduled for executionNullPointerException - if command is nullIllegalArgumentException - if period less than or equal to zero
/*
* 获取指定时间对应的毫秒数
* @param time "HH:mm:ss"
* @return
*/
private long getTimeMillis(String time) {
try {
DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");
Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);
return curDate.getTime();
} catch (Exception e) {
logger.error("getTimeMillis error:", e);
}
return 0;
}
public void execute() {
long oneDay = 24 * 60 * 60 * 1000;
long initDelay = getTimeMillis("05:00:00") - System.currentTimeMillis();
initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;
executorService.scheduleAtFixedRate(task, initDelay, oneDay, TimeUnit.MILLISECONDS);
}