Quartz执行任务排除指定日期
需求:定时任务执行时,在指定的日期不执行
执行的任务:
public class Test1 implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("定时任务执行了"); } }
设置排除日期:
注意:月份是从0开始
public class CalendarDemo { public static void main(String[] args) throws Exception { SchedulerFactory sf = new StdSchedulerFactory(); Scheduler scheduler = sf.getScheduler(); scheduler.start(); // 定义日历 AnnualCalendar holidays = new AnnualCalendar(); // 排除国庆节:2024-10-1 //注意:月份从0开始 Calendar day = (Calendar) new GregorianCalendar(2024, 9, 1); holidays.setDayExcluded(day, true); // 排除中秋节:2024-9-17 Calendar midAutumn = new GregorianCalendar(2024, 8, 17); holidays.setDayExcluded(midAutumn, true); // 调度器添加日历 scheduler.addCalendar("holidays", holidays, false, false); JobDetail jobDetail = JobBuilder.newJob(Demo1.class) .withIdentity("job1", "group1") .usingJobData("name","test") .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") .startNow() .modifiedByCalendar("holidays") .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(2) .repeatForever()) .build(); Date firstRunTime = scheduler.scheduleJob(jobDetail, trigger); System.out.println(jobDetail.getKey() + " 第一次触发: " + firstRunTime); } }