quartz 每月一次_每30天使用Quartz进行每月工作

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

My question is how to schedule this using cron trigger, having in mind that not all month have 31,30,29th days.

In such case job should run closest previous day of the month.

So, lets say April has only 30 days, so job has to run on 30th of April.

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**//);

I've got nextValidTime equal to 30th of March, so cron just "skipped" February.

Any help would be highly appreciated. Thanks in advance.

解决方案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.

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

Edit:

I would just do something like this then

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 ? *");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值