【问题标题】:Scheduler for a task to run at particular hour of the day for every N days每 N 天在一天中的特定时间运行任务的调度程序
【发布时间】:2014-06-27 19:07:49
【问题描述】:

我正在寻找一个调度程序,它应该在每 N 天指定的一天中的特定时间运行。

例如,我的任务应该每 10 天在晚上 11 点运行一次。

可以使用cron表达式配置小时,但是我们如何设置间隔。

感谢您的帮助

【问题讨论】:

标签: java scheduled-tasks


【解决方案1】:

您可以使用java.util.concurrent.ScheduledExecutorService

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

更新:要设置 initialDelay,您可以将 GregorianCalendar 中的时间设置为晚上 10 点并将其作为参数传递

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();
}

【讨论】:

    猜你喜欢
    • 2017-11-23
    • 2020-07-29
    • 2015-05-09
    • 1970-01-01
    • 2011-10-02
    • 2011-03-05
    • 1970-01-01
    • 2013-12-21
    相关资源
    最近更新 更多