简体   繁体   English

每N天在一天的特定时间运行任务的计划程序

[英]Scheduler for a task to run at particular hour of the day for every N days

I am looking for a scheduler which should run at a particular hour in the day for every N specified days. 我正在寻找一个调度程序,该调度程序应在指定的N天中每天的特定时间运行。

For example, my task should run at 11PM for every 10 days. 例如,我的任务应该每10天在晚上11点运行。

The hour can be configured using cron expression, but how do we set the interval. 可以使用cron表达式配置小时,但是如何设置间隔。

Thanks for the help 谢谢您的帮助

You can use java.util.concurrent.ScheduledExecutorService 您可以使用java.util.concurrent.ScheduledExecutorService

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(command, getTonight10PM(), period, unit);

Update: To set the initialDelay, you can set the time in GregorianCalendar as 10 PM and pass it as an argument 更新:要设置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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM