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