Calendar的使用

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;

/**
 * 
 * 每小时运行一次,并将五一和十一排除在外
 *
 */
public class CalendarExample {
	public static void main(String[] args) throws Exception {
		//通过SchedulerFactory获取一个调度器实例  
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		
		//法定节日是以每年为周期的,所以使用AnnualCalendar
		AnnualCalendar holidays = new AnnualCalendar();
		
		//五一劳动节
		Calendar laborDay = new GregorianCalendar();
		laborDay.add(Calendar.MONTH, 5);
		laborDay.add(Calendar.DATE, 1);
		
		//国庆节
		Calendar nationalDay = new GregorianCalendar();
		nationalDay.add(Calendar.MONTH, 10);
		nationalDay.add(Calendar.DATE, 1);
		
		ArrayList<Calendar> calendars = new ArrayList<Calendar>();
		calendars.add(laborDay);
		calendars.add(nationalDay);
		holidays.setDaysExcluded(calendars); //排除这两个节日
		
		//向Scheduler注册日历
		scheduler.addCalendar("holidays", holidays, false, false);
		
		Date runDate = TriggerUtils.getDateOf(0, 0, 10, 1, 4); //4月1日上午10点
		JobDetail jobDetail = new JobDetail("job1", "group1", SimpleJob.class);
		SimpleTrigger simpleTrigger = new SimpleTrigger("trigger1", "group1", runDate, 
				null, SimpleTrigger.REPEAT_INDEFINITELY, 60L * 60L * 1000L);
		simpleTrigger.setCalendarName("holidays"); //让Trigger应用指定的日历规则
		scheduler.scheduleJob(jobDetail, simpleTrigger);
		scheduler.start();
	}
}

 

你可能感兴趣的:(quartz)