Java 从现在开始每 14 分钟运行一次 Quartz cron 表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20417600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:48:12  来源:igfitidea点击:

Quartz cron expression to run job at every 14 minutes from now

javacronquartz-scheduler

提问by Vipul

I want to run a job at every 14 minutes form now.

我现在想每 14 分钟运行一次工作。

For example if I schedule a job at 11:04 am, using 0 0/14 * * * ?cron expression. then expected fire time suppose to be 11:18,11:32,11:46 and so on . but it will fire at 11:00,11:14:11,28:11:42,11:56,12:00 which is not expected. and why it fired at 12:00 o'clock after 11:56, there is diff of only 4 min.

例如,如果我在上午 11:04 安排工作,则使用0 0/14 * * * ?cron 表达式。然后预计开火时间假设为 11:18,11:32,11:46 等等。但它会在 11:00,11:14:11,28:11:42,11:56,12:00 触发,这不是预期的。以及为什么它在 11:56 之后的 12:00 点开火,只有 4 分钟的差异。

  • How can I achieve what I want using cron expression?
  • Can any one explain me this behaviour of quartz cron?
  • 如何使用 cron 表达式实现我想要的?
  • 任何人都可以向我解释石英 cron 的这种行为吗?

Thanks in advance.

提前致谢。

采纳答案by alexey28

Well, 0/14 give you fire time at 00, 14, 28, 42, 56 and again at 00 minutes of every hour. So last interval will be not 14 but 4 minutes. Thats how cron works. You can get equals interval in minutes only for cases when remainder of the division 60 by your interval is zero.

好吧,0/14 为您提供在 00、14、28、42、56 和每小时 00 分钟的点火时间。所以最后一次间隔将不是 14 分钟而是 4 分钟。这就是 cron 的工作原理。只有在除法 60 的余数除以您的间隔为零的情况下,您才能获得以分钟为单位的相等间隔。

回答by Ruchira Gayan Ranaweera

You get it wrong. 0/14means it will fire each hour starting from 0after 14min. That's why it is firing at 12.00

你弄错了。0/14意味着它将从0after开始每小时触发一次14min。这就是为什么它正在射击12.00

回答by shreyansh jogi

you should change your cronexpression to 0 0/14 * 1/1 * ? *

你应该改变你的cron表达0 0/14 * 1/1 * ? *

回答by Frederic Close

your cron expression should look like

你的 cron 表达式应该看起来像

0 0/14 * 1/1 * ? *

A great website to create your cron expression when you are in doubt : http://www.cronmaker.com/

一个很棒的网站,可以在您有疑问时创建您的 cron 表达式:http: //www.cronmaker.com/

it will help you build your cron expression and show you the next firing date times of your cron.

它将帮助您构建您的 cron 表达式并显示您的 cron 的下一个触发日期时间。

For More Reference : http://www.nncron.ru/help/EN/working/cron-format.htm

更多参考:http: //www.nncron.ru/help/EN/working/cron-format.htm

回答by Prabhakaran Ramaswamy

Use this cron expression.

使用这个 cron 表达式。

0 0/14 * ** ?

0 0/14 * ** ?

回答by Zaka

"0 0/14 * * * ?" means the next fire time from the beginning of the clock for every 14 minutes interval, like what you said.

“0 0/14 * * * ?” 表示每 14 分钟间隔从时钟开始的下一次触发时间,就像您说的那样。

The 1st '0' means SECOND at 0 (or 12) at the clock; and same for the 2nd '0' which means the MINUTE at 0 (or 12) at the clock; '/14' means 14 minutes as the interval.

第一个 '0' 表示时钟为 0(或 12)的第二个;和第二个“0”相同,这意味着时钟上的 MINUTE 为 0(或 12);'/14' 表示间隔为 14 分钟。

So get the SECOND and MINUTE from current timeand concatenate them with the interval into a cron expression then fire it. Below is the example for Java:

所以从当前时间获取 SECOND 和 MINUTE并将它们与间隔连接成一个 cron 表达式,然后触发它。下面是 Java 的例子:

public static String getCronExpressionFromNowWithSchedule(int minuteInterval) throws Exception {
    String cronExpression = "";
    Calendar now = Calendar.getInstance();
    int year = now.get(Calendar.YEAR);
    int month = now.get(Calendar.MONTH); // Note: zero based!
    int day = now.get(Calendar.DAY_OF_MONTH);
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int second = now.get(Calendar.SECOND);
    int millis = now.get(Calendar.MILLISECOND);

    if (minuteInterval<=0) cronExpression = (second+1)+" * * * * ?";
    else cronExpression = (second+1)+" "+minute+"/"+minuteInterval+" * * * ?";

    System.out.println(cronExpression);
    return cronExpression;
}

The next fire time is at next second from current time for the Minute interval you passed into this method.

下一次触发时间是您传入此方法的分钟间隔的当前时间的下一秒。