当您说它不起作用时,您会得到什么错误?
您可以尝试以下代码( 编辑: 适用于Quartz
2.2)。此方法未在cron表达式中指定开始/结束日期和年份,而是使用Trigger方法指定它们。(注意:我自己尚未测试过,请告诉我它是否对您有用)
编辑: 我有机会测试此代码,我在下面的代码中运行并不断更改系统时钟,并且从开始到结束日期的上午9点至12点之间,所有触发器均成功。
public class CronJob { public static void main(String[] args) throws ParseException, SchedulerException { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail job = newJob(TestJob.class) .withIdentity("cronJob", "testJob") .build(); String startDateStr = "2013-09-27 00:00:00.0"; String endDateStr = "2013-09-31 00:00:00.0"; Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr); Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr); CronTrigger cronTrigger = newTrigger() .withIdentity("trigger1", "testJob") .startAt(startDate) .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing()) .endAt(endDate) .build(); scheduler.scheduleJob(job, cronTrigger); scheduler.start(); } public static class TestJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("this is a cron scheduled test job"); } }}如果上面的代码不能正常工作,尝试更换
cronSchedule("0 0 9-12 * * ?")与cronSchedule("0 0 9-12 ? *?")


