我正在寻找一个调度程序,该调度程序应在指定的N天中的一天中的特定时间运行。
例如,我的任务应该每10天在晚上11点运行一次。
可以使用cron表达式配置小时,但是如何设置时间间隔。
谢谢您的帮助
您可以使用 java.util.concurrent.ScheduledExecutorService
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(command, getTonight10PM(), period, unit);
更新:要设置initialDelay,您可以将GregorianCalendar中的时间设置为10 PM并将其作为参数传递
private static Date getTonight10PM() {
Calendar today = new GregorianCalendar();
Calendar result =
new GregorianCalendar(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE), 23, 0);
return result.getTime();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句