每30天使用Quartz进行每月工作 [英] Monthly jobs on every 30th day using Quartz

查看:302
本文介绍了每30天使用Quartz进行每月工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

我有按用户计划的每月工作(使用Quartz)。用户提供开始日期f
或要运行的第一份工作,可能是每月1-31日的任何一天

guys,

I have monthly jobs scheduled(using Quartz) by users. Users provide starting date f or first job to run, it could be any day of month 1-31

可以使用单个cron触发器吗?还是应该结合使用触发器?
我尝试使用CronExpression来查看它如何处理此类情况:

Can it be done using single cron trigger? Or should it be combination of triggers? I tried to play with CronExpression to see how it handles such cases:

CronExpression ce = new CronExpression("0 0 0 30 JAN-DEC ? *");
Date nextValidTime = ce.getNextValidTimeAfter(//**27th of February**//);

我的nextValidTime等于 3月30日,所以cron只是2月跳过。
任何帮助将不胜感激。预先感谢。

I've got nextValidTime equal to 30th of March, so cron just "skipped" February. Any help would be highly appreciated. Thanks in advance.

推荐答案


该月的一天中允许使用'L'字符和星期几字段。该字符>是 last的缩写,但在两个字段中的每个含义都不同。例如,对于
,月中天字段中的值 L表示该月的最后一天
-1月的第31天,2月的第28天(非跨月$ b $) b年。如果单独在星期几字段中使用,则仅表示 7
或 SAT。但是,如果在星期几字段中使用另一个值,则
表示该月的最后一个xxx天-例如 6L表示
该月的最后一个星期五。您还可以指定与每月
的偏移量,例如 L-3,这表示日历月的倒数第二个
。使用 L选项时,不要指定列表或值的范围,
很重要,因为这样会导致
令人困惑/意外的结果。

The 'L' character is allowed for the day-of-month and day-of-week fields. This character > is short-hand for "last", but it has different meaning in each of the two fields. 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". You can also specify an offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the calendar month. When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing/unexpected results.

http://quartz-scheduler.org/ api / 2.0.0 / org / quartz / CronExpression.html



new CronExpression("0 0 0 L JAN-DEC ? *");

编辑:

然后我会做类似的事情

Calendar tCalendar = Calendar.getInstance();
tCalendar.set(2009, Calendar.FEBRUARY/*int*/, 1); // for example Feb, 2009 -- day doesn't matter here
if(userSelectedDay > tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) ){
    //Fix user day
    //fixedDay = tCalendar.getActualMaximum(Calendar.DAY_OF_MONTH)

    // Or, for that month 
    //new CronExpression("0 0 0 L JAN-DEC ? *");
}

这篇关于每30天使用Quartz进行每月工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆