Cron Expressions
cron的表達式被用來配置CronTrigger實例。
cron的表達式是字符串,實際上是由七子表達式,描述個別細節的時間表。
這些子表達式是分開的空白,代表:
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (可選字段)
例 "0 0 12 ? * WED" 在每星期三下午12:00 執行,個別子表達式可以包含范圍;
例如,在前面的例子里("WED")可以替換成 "MON-FRI", "MON, WED, FRI"甚至"MON-WED,SAT".“*” 代表整個時間段.
每一個字段都有一套可以指定有效值,如
Seconds (秒) :可以用數字0-59 表示, Minutes(分) :可以用數字0-59 表示, Hours(時) :可以用數字0-23表示, Day-of-Month(天) :可以用數字1-31 中的任一一個值,但要注意一些特別的月份 Month(月) :可以用0-11 或用字符串 “JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC” 表示 Day-of-Week(每周) :可以用數字1-7表示(1 = 星期日)或用字符口串“SUN, MON, TUE, WED, THU, FRI and SAT”表示
“*”:指定所有的值,比如,Minutes 設置為 *,表示每分鍾
“/”:為特別單位,表示為“每”如“0/15”表示每隔15分鍾執行一次,“0”表示為從“0”分開始, “3/20”表示表示每隔20分鍾執行一次,“3”表示從第3分鍾開始執行
“?”:表示每月的某一天,或第周的某一天
“L”:用於每月,或每周,表示為每月的最后一天,或每個月的最后星期幾如“6L”表示“每月的最后一個星期五”
“W”:表示為最近工作日,如“15W”放在每月(day-of-month)字段上表示為“到本月15日最近的工作日”
“#”:是用來指定“的”每月第n個工作日,例 在每周(day-of-week)這個字段中內容為"6#3" or "FRI#3" 則表示“每月第三個星期五”
注意: Day-of-Month 和 Day-of-Week不可同時為 * ,否則報錯:

1 java.lang.RuntimeException: CronExpression '0/2 * * * * * *' is invalid. 2 3 at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:111) 4 at quartz.QuartzExample.getTrigger(QuartzExample.java:110) 5 at quartz.QuartzExample.run(QuartzExample.java:74) 6 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 7 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 8 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 9 at java.lang.reflect.Method.invoke(Method.java:606) 10 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 11 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 12 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 13 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 14 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 15 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 16 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 17 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 18 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 19 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 20 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 21 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 22 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 23 at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 24 at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 25 at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 26 at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 27 at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 28 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 29 Caused by: java.text.ParseException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented. 30 at org.quartz.CronExpression.buildExpression(CronExpression.java:511) 31 at org.quartz.CronExpression.<init>(CronExpression.java:276) 32 at org.quartz.CronScheduleBuilder.cronSchedule(CronScheduleBuilder.java:107) 33 ... 25 more 34 35 Disconnected from the target VM, address: '127.0.0.1:61437', transport: 'socket' 36 37 Process finished with exit code -1
CronExpression 源碼:

protected void buildExpression(String expression) throws ParseException { expressionParsed = true; try { if (seconds == null) { seconds = new TreeSet<Integer>(); } if (minutes == null) { minutes = new TreeSet<Integer>(); } if (hours == null) { hours = new TreeSet<Integer>(); } if (daysOfMonth == null) { daysOfMonth = new TreeSet<Integer>(); } if (months == null) { months = new TreeSet<Integer>(); } if (daysOfWeek == null) { daysOfWeek = new TreeSet<Integer>(); } if (years == null) { years = new TreeSet<Integer>(); } int exprOn = SECOND; StringTokenizer exprsTok = new StringTokenizer(expression, " \t", false); while (exprsTok.hasMoreTokens() && exprOn <= YEAR) { String expr = exprsTok.nextToken().trim(); // throw an exception if L is used with other days of the month if(exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' and 'LW' with other days of the month is not implemented", -1); } // throw an exception if L is used with other days of the week if(exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && expr.contains(",")) { throw new ParseException("Support for specifying 'L' with other days of the week is not implemented", -1); } if(exprOn == DAY_OF_WEEK && expr.indexOf('#') != -1 && expr.indexOf('#', expr.indexOf('#') +1) != -1) { throw new ParseException("Support for specifying multiple \"nth\" days is not implemented.", -1); } StringTokenizer vTok = new StringTokenizer(expr, ","); while (vTok.hasMoreTokens()) { String v = vTok.nextToken(); storeExpressionVals(0, v, exprOn); } exprOn++; } if (exprOn <= DAY_OF_WEEK) { throw new ParseException("Unexpected end of expression.", expression.length()); } if (exprOn <= YEAR) { storeExpressionVals(0, "*", YEAR); } TreeSet<Integer> dow = getSet(DAY_OF_WEEK); TreeSet<Integer> dom = getSet(DAY_OF_MONTH); // Copying the logic from the UnsupportedOperationException below boolean dayOfMSpec = !dom.contains(NO_SPEC); boolean dayOfWSpec = !dow.contains(NO_SPEC); if (!dayOfMSpec || dayOfWSpec) { if (!dayOfWSpec || dayOfMSpec) { throw new ParseException( "Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0); } } } catch (ParseException pe) { throw pe; } catch (Exception e) { throw new ParseException("Illegal cron expression format (" + e.toString() + ")", 0); } }
1)Cron表達式的格式:秒 分 時 日 月 周 年(可選)。
字段名 允許的值 允許的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小時 0-23 , - * /
日 1-31 , - * ? / L W C
月 1-12 or JAN-DEC , - * /
周幾 1-7 or SUN-SAT , - * ? / L C #
年 (可選字段) empty, 1970-2099 , - * /
“?”字符:表示不確定的值
“,”字符:指定數個值
“-”字符:指定一個值的范圍
“/”字符:指定一個值的增加幅度。n/m表示從n開始,每次增加m
“L”字符:用在日表示一個月中的最后一天,用在周表示該月最后一個星期X
“W”字符:指定離給定日期最近的工作日(周一到周五)
“#”字符:表示該月第幾個周X。6#3表示該月第3個周五
2)Cron表達式范例:
每隔5秒執行一次:*/5 * * * * ? 每隔1分鍾執行一次:0 */1 * * * ? 每天23點執行一次:0 0 23 * * ? 每天凌晨1點執行一次:0 0 1 * * ? 每月1號凌晨1點執行一次:0 0 1 1 * ? 每月最后一天23點執行一次:0 0 23 L * ? 每周星期天凌晨1點實行一次:0 0 1 ? * L 在26分、29分、33分執行一次:0 26,29,33 * * * ? 每天的0點、13點、18點、21點都執行一次:0 0 0,13,18,21 * * ?
每天的7點到21點都執行一次:0 0 7-21 * * ?
轉載自:http://www.cnblogs.com/sunjie9606/archive/2012/03/15/2397626.html