Quartz cron - 如果月份中的某一天不存在怎么办?

发布于 2024-12-22 00:41:53 字数 193 浏览 1 评论 0原文

我正在尝试为石英调度程序编写一个简单的 cron 表达式。我希望该作业在每月第 30 天凌晨 3 点运行。

0 0 3 30 JAN-DEC ? *

我想知道二月会发生什么?作业会运行还是不会运行?

我不是在寻找每月最后一天的解决方案,我需要用户选择作业运行的日期(最好是所有月份一次)。

I'm trying to write a simple cron expression for quartz scheduler. I want the job to run every month on day 30 at 3am.

0 0 3 30 JAN-DEC ? *

I wonder what happens for the month of February? Will the job run or not run?

I'm not looking for a last day of month solution, I need the user to select the day of month when the job will run (ideally once for all months).

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

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

发布评论

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

评论(4

☆獨立☆ 2024-12-29 00:41:53

L(“最后”)- 在允许使用的两个字段中具有不同的含义。例如,日期字段中的值“L”表示“该月的最后一天” - 对于非闰年,一月为第 31 天,二月为第 28 天。如果单独用于星期几字段,则它仅表示“7”或“SAT”。但如果在星期几字段中使用另一个值之后,则表示“该月的最后 xxx 天” - 例如“6L”表示“该月的最后一个星期五”。使用“L”选项时,重要的是不要指定列表或值范围,因为您会得到令人困惑的结果。

您可以使用它来指定,而不是直接在玉米作业中指定 30。

http://www.quartz-scheduler.org/documentation/quartz -1.x/tutorials/crontrigger

检查特殊字符。

谢谢。

L ("last") - has different meaning in each of the two fields in which it is allowed. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

You can use this to specify instead of specifying 30 in your corn job directly.

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

Check for Special characters.

Thanks.

橘寄 2024-12-29 00:41:53

它不会运行。如果您希望它在 2 月的 28 日运行,则必须为一个月中的每种情况创建多个 CronExpressions,并为每个触发器创建一个触发器,然后将所有触发器添加到所需的作业中。

这就是我所做的:

CronExpressions 创建:

public static List<CronExpression> getCronExpressionList(int seconds, int minutes,
            int hours, int dayInMonth, Month month,
            DayOfWeek dayOfWeek) {
    final String monthsWith30Days = Month.APR + "," + Month.JUN + ","
                    + Month.SEP + "," + Month.NOV;
    List<CronExpression> crons = new LinkedList<CronExpression>();

    String timeString = String.format(("%s %s %s "), seconds, minutes,
                    hours, 0, 0, 0);
    String dateString = "%s %s %s";
    String cron = null;

    cron = timeString + String.format(dateString, dayInMonth, "*", "?");
    crons.add(new CronExpression(cron));
    if (dayInMonth > 28) {
        String febCron = timeString + getFebruarLastDayDateString(dateString);
        crons.add(new CronExpression(febCron));
        if (dayInMonth == 31) {
            String monthsWithThirtyDaysCron = timeString + String.format(dateString,
                    "L", monthsWith30Days, "?");
            crons.add(new CronExpression(monthsWithThirtyDaysCron));
        }
    }
    return crons;
}

private static String getFebruarLastDayDateString(String initialCron) 
               throws ParseException {
    return String.format(initialCron, "L", Month.FEB, "?");
}

触发器创建:

        Set<CronTrigger> triggers = new HashSet<>();

        int i = 1;
        for (CronExpression cronEx : cronsList) {
            CronTrigger trigger = newTrigger()
                    .withIdentity("trigger" + i, groupName)
                    .withSchedule(cronSchedule(cronEx))
                    .build();
                triggers.add(trigger);
                i++;
        }

It won't run. If you want it to run in the 28th in case of February, you have to create multiple CronExpressions for each case of days in month, and a trigger for each one, and then add all the triggers to your required Job.

This is what I have done:

CronExpressions creation:

public static List<CronExpression> getCronExpressionList(int seconds, int minutes,
            int hours, int dayInMonth, Month month,
            DayOfWeek dayOfWeek) {
    final String monthsWith30Days = Month.APR + "," + Month.JUN + ","
                    + Month.SEP + "," + Month.NOV;
    List<CronExpression> crons = new LinkedList<CronExpression>();

    String timeString = String.format(("%s %s %s "), seconds, minutes,
                    hours, 0, 0, 0);
    String dateString = "%s %s %s";
    String cron = null;

    cron = timeString + String.format(dateString, dayInMonth, "*", "?");
    crons.add(new CronExpression(cron));
    if (dayInMonth > 28) {
        String febCron = timeString + getFebruarLastDayDateString(dateString);
        crons.add(new CronExpression(febCron));
        if (dayInMonth == 31) {
            String monthsWithThirtyDaysCron = timeString + String.format(dateString,
                    "L", monthsWith30Days, "?");
            crons.add(new CronExpression(monthsWithThirtyDaysCron));
        }
    }
    return crons;
}

private static String getFebruarLastDayDateString(String initialCron) 
               throws ParseException {
    return String.format(initialCron, "L", Month.FEB, "?");
}

Trigger creation:

        Set<CronTrigger> triggers = new HashSet<>();

        int i = 1;
        for (CronExpression cronEx : cronsList) {
            CronTrigger trigger = newTrigger()
                    .withIdentity("trigger" + i, groupName)
                    .withSchedule(cronSchedule(cronEx))
                    .build();
                triggers.add(trigger);
                i++;
        }
眼眸印温柔 2024-12-29 00:41:53

检查简单代码

public class TestCronTrigger {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws Exception {

        String dateStr = "2015-02-10";
        String cron = "0 0 0 31 * ?";
        Date nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-02-10";
        cron = "0 0 0 30 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-02-10";
        cron = "0 0 0 28 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-03-10";
        cron = "0 0 0 31 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

    }

    static Date getNextFireTimeFromDateForCron(Date from, String cron) throws ParseException {
        CronTriggerImpl cronTrigger = new CronTriggerImpl();
        cronTrigger.setCronExpression(cron);
        cronTrigger.setStartTime(from);
        return cronTrigger.computeFirstFireTime(null);
    }
}

对于此代码,输出将是:

For cron '0 0 0 31 * ?' next fire time after '2015-02-10' will be '2015-03-31'
For cron '0 0 0 30 * ?' next fire time after '2015-02-10' will be '2015-03-30'
For cron '0 0 0 28 * ?' next fire time after '2015-02-10' will be '2015-02-28'
For cron '0 0 0 31 * ?' next fire time after '2015-03-10' will be '2015-03-31'

玩弄参数,你可以找到答案

check simple code

public class TestCronTrigger {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

    public static void main(String[] args) throws Exception {

        String dateStr = "2015-02-10";
        String cron = "0 0 0 31 * ?";
        Date nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-02-10";
        cron = "0 0 0 30 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-02-10";
        cron = "0 0 0 28 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

        dateStr = "2015-03-10";
        cron = "0 0 0 31 * ?";
        nextFireTime = getNextFireTimeFromDateForCron(DATE_FORMAT.parse(dateStr), cron);
        System.out.println(String.format("For cron '%s' next fire time after '%s' will be '%s'", cron, dateStr, DATE_FORMAT.format(nextFireTime)));

    }

    static Date getNextFireTimeFromDateForCron(Date from, String cron) throws ParseException {
        CronTriggerImpl cronTrigger = new CronTriggerImpl();
        cronTrigger.setCronExpression(cron);
        cronTrigger.setStartTime(from);
        return cronTrigger.computeFirstFireTime(null);
    }
}

For this code the output will be:

For cron '0 0 0 31 * ?' next fire time after '2015-02-10' will be '2015-03-31'
For cron '0 0 0 30 * ?' next fire time after '2015-02-10' will be '2015-03-30'
For cron '0 0 0 28 * ?' next fire time after '2015-02-10' will be '2015-02-28'
For cron '0 0 0 31 * ?' next fire time after '2015-03-10' will be '2015-03-31'

playing with arguments you can find answers

清晨说晚安 2024-12-29 00:41:53

下面的代码计算每月最后一天的下一次火灾日期

static Date getNextFireTimeFromDateForCron() throws ParseException {
    CronTriggerImpl cronTrigger = new CronTriggerImpl();
    cronTrigger.setCronExpression("0 15 15 L * ?");
    cronTrigger.setStartTime(new Date());
    return cronTrigger.computeFirstFireTime(null);
}

The below code calculates the next fire date for the last day of every month

static Date getNextFireTimeFromDateForCron() throws ParseException {
    CronTriggerImpl cronTrigger = new CronTriggerImpl();
    cronTrigger.setCronExpression("0 15 15 L * ?");
    cronTrigger.setStartTime(new Date());
    return cronTrigger.computeFirstFireTime(null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文