quartz时间表达式Cron详解
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不可同时为 *,否则报错:
java.lang.RuntimeException: CronExpression '0/2 * * * * * *' is invalid.
at org.Schedule(CronScheduleBuilder.java:111)
at Trigger(QuartzExample.java:110)
at quartz.QuartzExample.run(QuartzExample.java:74)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:606)
at org.del.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.del.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
生活中的美作文
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at cution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at cution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at cution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caud by: ParException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.
at org.quartz.CronExpression.buildExpression(CronExpression.java:511)
at org.quartz.CronExpression.<init>(CronExpression.java:276)
at org.Schedule(CronScheduleBuilder.java:107)
... 25 more
Disconnected from the target VM, address: '127.0.0.1:61437', transport: 'socket'
Process finished with exit code -1
CronExpression 源码:
protected void buildExpression(String expression) throws ParException {
expressionPard = true;
try {
if (conds == null) {
conds = 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",
fal);
while (exprsTok.hasMoreTokens() && exprOn <= YEAR) {
String expr = Token().trim();
// throw an exception if L is ud with other days of the month
if(exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && ains(",")) {
throw new ParException("Support for specifying 'L' and 'LW' with other days of the month is not implemented", -1); }
// throw an exception if L is ud with other days of the week
if(exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && ains(",")) {
throw new ParException("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 ParException("Support for specifying multiple \"nth\" days is not implemented.", -1);
}
StringTokenizer vTok = new StringTokenizer(expr, ",");
while (vTok.hasMoreTokens()) {
String v = Token();
storeExpressionVals(0, v, exprOn);
}
g邮箱exprOn++;
}
if (exprOn <= DAY_OF_WEEK) {
throw new ParException("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 = !ains(NO_SPEC);
boolean dayOfWSpec = !ains(NO_SPEC);
if (!dayOfMSpec || dayOfWSpec) {
if (!dayOfWSpec || dayOfMSpec) {
throw new ParException(
"Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0); }
}
} catch (ParException pe) {
throw pe;
} catch (Exception e) {
防火作文throw new ParException("Illegal cron expression format ("
+ e.toString() + ")", 0);
}
}
CronExpression 部分代码
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 * * ?
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。