我在使用 HolidayCalendar 时遇到问题。似乎日历被简单地忽略了。
这是我的代码(主要重复 Quartz 文档中的代码片段):
public class Cron {
public static void main(String[] args) {
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
Calendar cbegin = Calendar.getInstance(), cend = Calendar.getInstance();
cbegin.set(Calendar.MONTH, Calendar.NOVEMBER);
cend.set(Calendar.MONTH, Calendar.NOVEMBER);
// exclude Nov 23 and Nov 24
HolidayCalendar cln = new HolidayCalendar();
cbegin.set(Calendar.DAY_OF_MONTH, 23);
cln.addExcludedDate(new Date(cbegin.getTimeInMillis()));
cbegin.set(Calendar.DAY_OF_MONTH, 24);
cln.addExcludedDate(new Date(cbegin.getTimeInMillis()));
scheduler.addCalendar("job1", cln, false, false);
JobDetail job = newJob(HelloJob.class).withIdentity("job1").build();
// trigger running once a day at 1 AM
Trigger trigger = newTrigger().withIdentity("trigger10").forJob(job)
.withSchedule(cronSchedule("0 0 1 * * ?"))
.modifiedByCalendar("job1")
.build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
// print fired times from Nov 21 to Nov 30
cbegin.set(Calendar.DAY_OF_MONTH, 21);
cend.set(Calendar.DAY_OF_MONTH, 30);
Date dtnext = new Date(cbegin.getTimeInMillis()), dtend = new Date(cend.getTimeInMillis());
do {
dtnext = trigger.getFireTimeAfter(dtnext);
System.out.println("Next... " + dtnext);
} while(dtnext.before(dtend));
try { Thread.sleep(3600000); } catch(InterruptedException ie) {}
scheduler.shutdown();
} catch (Exception se) {
se.printStackTrace();
}
}
}
结果是:
[java] Next... Thu Nov 22 01:00:00 CET 2012
[java] Next... Fri Nov 23 01:00:00 CET 2012
[java] Next... Sat Nov 24 01:00:00 CET 2012
[java] Next... Sun Nov 25 01:00:00 CET 2012
[java] Next... Mon Nov 26 01:00:00 CET 2012
[java] Next... Tue Nov 27 01:00:00 CET 2012
[java] Next... Wed Nov 28 01:00:00 CET 2012
[java] Next... Thu Nov 29 01:00:00 CET 2012
[java] Next... Fri Nov 30 01:00:00 CET 2012
[java] Next... Sat Dec 01 01:00:00 CET 2012
如您所见,11 月 23 日和 11 月 24 日的排除日期仍然排定。
我的代码有什么问题?