将 Java 任务安排在指定时间

发布于 2024-12-14 22:54:57 字数 187 浏览 1 评论 0原文

我希望能够在 Java 中在特定时间安排任务。我知道 ExecutorService 能够定期在指定的延迟后进行调度,但我更多的是在一天中的某个时间而不是在一段持续时间之后进行调度。

有没有办法让 Runnable 在 2:00 执行,或者我是否需要计算从现在到 2:00 之间的时间,然后安排可运行在该延迟之后执行?

I would like to be able to schedule a task at a specific time in Java. I understand that the ExecutorService has the ability to schedule at periodic intervals, and after a specified delay, but I am looking more for a time of day as opposed to after a duration.

Is there a way to have, say, a Runnable execute at 2:00, or do I need to calculate the time between now and 2:00, and then schedule the runnable to execute after that delay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

箜明 2024-12-21 22:54:58

查看 Quartz。我们将它用于我们的生产应用程序,它非常好。它的工作原理与 crontab 非常相似。您可以在设定的计划中指定一个时间,它将在那时执行回调。

Check out Quartz. We use it for our production apps, and it's very good. It works pretty much like crontab. You can specify a time during a set schedule and it'll execute a callback at that time.

情话墙 2024-12-21 22:54:58

用户java.util.Timer。它有方法 new schedule(task, ti​​me) ,其中 time 是您想要执行一次任务的日期。

user java.util.Timer. It has method new schedule(task, time) where time is a Date when you want to execute the task once.

南…巷孤猫 2024-12-21 22:54:57

您也可以使用 spring 注释

@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}

http:// /static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

you can use spring annotations too

@Scheduled(cron="*/5 * * * * MON-FRI")
public void doSomething() {
// something that should execute on weekdays only
}

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html

不奢求什么 2024-12-21 22:54:57

这就是我使用 java7SE 解决它的方法:

    timer = new Timer("Timer", true);
    Calendar cr = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cr.setTimeInMillis(System.currentTimeMillis());
    long day = TimeUnit.DAYS.toMillis(1);
    //Pay attention - Calendar.HOUR_OF_DAY for 24h day model 
    //(Calendar.HOUR is 12h model, with p.m. a.m. )
    cr.set(Calendar.HOUR_OF_DAY, it.getHours());
    cr.set(Calendar.MINUTE, it.getMinutes());
    long delay = cr.getTimeInMillis() - System.currentTimeMillis();
    //insurance for case then time of task is before time of schedule
    long adjustedDelay = (delay > 0 ? delay : day + delay);
    timer.scheduleAtFixedRate(new StartReportTimerTask(it), adjustedDelay, day);
    //you can use this schedule instead is sure your time is after current time
    //timer.scheduleAtFixedRate(new StartReportTimerTask(it), cr.getTime(), day);

它恰好比我想象的正确执行更棘手

this is how I've solved it using java7SE:

    timer = new Timer("Timer", true);
    Calendar cr = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cr.setTimeInMillis(System.currentTimeMillis());
    long day = TimeUnit.DAYS.toMillis(1);
    //Pay attention - Calendar.HOUR_OF_DAY for 24h day model 
    //(Calendar.HOUR is 12h model, with p.m. a.m. )
    cr.set(Calendar.HOUR_OF_DAY, it.getHours());
    cr.set(Calendar.MINUTE, it.getMinutes());
    long delay = cr.getTimeInMillis() - System.currentTimeMillis();
    //insurance for case then time of task is before time of schedule
    long adjustedDelay = (delay > 0 ? delay : day + delay);
    timer.scheduleAtFixedRate(new StartReportTimerTask(it), adjustedDelay, day);
    //you can use this schedule instead is sure your time is after current time
    //timer.scheduleAtFixedRate(new StartReportTimerTask(it), cr.getTime(), day);

it happens to be trickier than I thought to do it correctly

奢华的一滴泪 2024-12-21 22:54:57

您将需要 Quartz

You'll be wanting Quartz.

背叛残局 2024-12-21 22:54:57

今天早上我遇到了这种情况......这是我在午夜运行的代码

    scheduler = Executors.newScheduledThreadPool(1);
    Long midnight=LocalDateTime.now().until(LocalDate.now().plusDays(1).atStartOfDay(), ChronoUnit.MINUTES);
    scheduler.scheduleAtFixedRate(this, midnight, 1440,  TimeUnit.MINUTES);

Got myself on this situation this morning... This was my code to run at midnight

    scheduler = Executors.newScheduledThreadPool(1);
    Long midnight=LocalDateTime.now().until(LocalDate.now().plusDays(1).atStartOfDay(), ChronoUnit.MINUTES);
    scheduler.scheduleAtFixedRate(this, midnight, 1440,  TimeUnit.MINUTES);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文