CronExpression_CronExpression 规则

字段 允许值 允许的特殊字符   
秒   0-59   , - * /   
分   0-59   , - * /   
小时 0-23   , - * /   
日期 1-31   , - * ? / L W C   
月份 1-12 或者 JAN-DEC , - * /   
星期 1-7 或者 SUN-SAT , - * ? / L C #   
年(可选) 留空, 1970-2099 , - * /
   
表达式意义     
"0 15 10 ? * *" 每天上午10:15触发   
"0 15 10 * * ?" 每天上午10:15触发   
"0 15 10 * * ? *" 每天上午10:15触发   
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发   
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发   
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发   
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发   
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发   
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发   
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发   
"0 15 10 15 * ?" 每月15日上午10:15触发   
"0 15 10 L * ?" 每月最后一日的上午10:15触发   
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发   
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发  

'*' 字符可以用于所有字段,在“分”字段中设为"*"表示"每一分钟"的含义。

'?' 字符可以用在“日”和“周几”字段. 它用来指定 '不明确的值'. 这在你需要指定这两个字段中的某一个值而不是另外一个的时候会被用

到。在后面的例子中可以看到其含义。

'-' 字符被用来指定一个值的范围,比如在“小时”字段中设为"10-12"表示"10点到12点".

',' 字符指定数个值。比如在“周几”字段中设为"MON,WED,FRI"表示"the days Monday, Wednesday, and Friday".

'/' 字符用来指定一个值的的增加幅度. 比如在“秒”字段中设置为"0/15"表示"第0, 15, 30, 和 45秒"。而 "5/15"则表示"第5, 20, 35, 和

50". 在'/'前加"*"字符相当于指定从0秒开始. 每个字段都有一系列可以开始或结束的数值。对于“秒”和“分”字段来说,其数值范围为0到

59,对于“小时”字段来说其为0到23, 对于“日”字段来说为0到31, 而对于“月”字段来说为1到12。"/"字段仅仅只是帮助你在允许的数值

范围内从开始"第n"的值。 因此 对于“月”字段来说"7/6"只是表示7月被开启而不是“每六个月”, 请注意其中微妙的差别。

'L'字符可用在“日”和“周几”这两个字段。它是"last"的缩写, 但是在这两个字段中有不同的含义。例如,“日”字段中的"L"表示"一个月

中的最后一天" —— 对于一月就是31号对于二月来说就是28号(非闰年)。而在“周几”字段中, 它简单的表示"7" or "SAT",但是如果在“

周几”字段中使用时跟在某个数字之后, 它表示"该月最后一个星期×" —— 比如"6L"表示"该月最后一个周五"。当使用'L'选项时,指定确定

的列表或者范围非常重要,否则你会被结果搞糊涂的。

'W' 可用于“日”字段。用来指定历给定日期最近的工作日(周一到周五) 。比如你将“日”字段设为"15W",意为: "离该月15号最近的工作日

"。因此如果15号为周六,触发器会在14号即周五调用。如果15号为周日, 触发器会在16号也就是周一触发。如果15号为周二,那么当天就会触

发。然而如果你将“日”字段设为"1W", 而一号又是周六, 触发器会于下周一也就是当月的3号触发,因为它不会越过当月的值的范围边界。'W'

字符只能用于“日”字段的值为单独的一天而不是一系列值的时候。

'L'和'W'可以组合用于“日”字段表示为'LW',意为"该月最后一个工作日"。

'#' 字符可用于“周几”字段。该字符表示“该月第几个周×”,比如"6#3"表示该月第三个周五( 6表示周五而"#3"该月第三个)。再比如:

"2#1" = 表示该月第一个周一而 "4#5" = 该月第五个周三。注意如果你指定"#5"该月没有第五个“周×”,该月是不会触发的。

'C' 字符可用于“日”和“周几”字段,它是"calendar"的缩写。 它表示为基于相关的日历所计算出的值(如果有的话)。如果没有关联的日

历, 那它等同于包含全部日历。“日”字段值为"5C"表示"日历中的第一天或者5号以后",“周几”字段值为"1C"则表示"日历中的第一天或者

周日以后"。

对于“月份”字段和“周几”字段来说合法的字符都不是大小写敏感的。

 

验证CronExpression表达式合法性的j函数 并非正则表达式

  1. function cronValidate(cronExpression ){     
  2.         //alert("校验函数的开始!");     
  3.         var cronParams = cronExpression.split(" ");     
  4.     
  5.         if (cronParams.length < 6 || cronParams.length > 7) {     
  6.             return false;     
  7.         }     
  8.     
  9.         //CronTrigger cronTrigger = new CronTrigger();     
  10.         //cronTrigger.setCronExpression( cronExpression );     
  11.     
  12.         if (cronParams[3] == "?" || cronParams[5]=="?") {     
  13.             //Check seconds param     
  14.             if (!checkSecondsField(cronParams[0])) {     
  15.                 return false;     
  16.             }     
  17.     
  18.             //Check minutes param     
  19.             if (!checkMinutesField(cronParams[1])) {     
  20.                 return false;     
  21.             }     
  22.     
  23.             //Check hours param     
  24.             if (!checkHoursField(cronParams[2])) {     
  25.                 return false;     
  26.             }     
  27.     
  28.             //Check day-of-month param     
  29.             if (!checkDayOfMonthField(cronParams[3])) {     
  30.                 return false;     
  31.             }     
  32.     
  33.             //Check months param     
  34.             if (!checkMonthsField(cronParams[4])) {     
  35.                 return false;     
  36.             }     
  37.     
  38.             //Check day-of-week param     
  39.             if (!checkDayOfWeekField(cronParams[5])) {     
  40.                 return false;     
  41.             }     
  42.     
  43.             //Check year param     
  44.             if (cronParams.length == 7) {     
  45.                 if (!checkYearField(cronParams[6])) {     
  46.                     return false;     
  47.                 }     
  48.             }     
  49.     
  50.             return true;     
  51.         } else {     
  52.             return false;     
  53.         }     
  54.     }     
  55.     
  56.     function checkSecondsField(secondsField) {     
  57.         return checkField(secondsField, 0, 59);     
  58.     }     
  59.     
  60.     
  61.     function checkField(secondsField, minimal, maximal) {     
  62.         if (secondsField.indexOf("-") > -1 ) {     
  63.             var startValue = secondsField.substring(0, secondsField.indexOf( "-" ));     
  64.             var endValue = secondsField.substring(secondsField.indexOf( "-" ) + 1);     
  65.     
  66.             if (!(checkIntValue(startValue, minimal, maximal, true) && checkIntValue(endValue, minimal, maximal, true))) {     
  67.                 return false;     
  68.             }     
  69.             try {     
  70.                 var startVal = parseInt(startValue, 10);     
  71.                 var endVal = parseInt(endValue, 10);     
  72.     
  73.                 return endVal > startVal;     
  74.             } catch (e) {     
  75.                 return false;     
  76.             }     
  77.         } else if (secondsField.indexOf(",") > -1) {     
  78.             return checkListField(secondsField, minimal, maximal);     
  79.         } else if (secondsField.indexOf( "/" ) > -1) {     
  80.             return checkIncrementField( secondsField, minimal, maximal );     
  81.         } else if (secondsField.indexOf( "*" ) != -1) {     
  82.             return true;     
  83.         } else {     
  84.             return checkIntValue(secondsField, minimal, maximal);     
  85.         }     
  86.     }     
  87.     
  88.     function checkIntValue(value, minimal, maximal, checkExtremity) {     
  89.         try {     
  90.             var val = parseInt(value, 10);     
  91.             //判断是否为整数     
  92.             if (value == val) {     
  93.                 if (checkExtremity) {     
  94.                     if (val < minimal || val > maximal) {     
  95.                         return false;     
  96.                     }     
  97.                 }     
  98.     
  99.                 return true;     
  100.             }     
  101.     
  102.             return false;     
  103.         } catch (e) {     
  104.             return false;     
  105.         }     
  106.     }     
  107.     
  108.     function checkMinutesField(minutesField) {     
  109.         return checkField(minutesField, 0, 59);     
  110.     }     
  111.     
  112.     function checkHoursField(hoursField) {     
  113.         return checkField(hoursField, 0, 23);     
  114.     }     
  115.     
  116.     function checkDayOfMonthField(dayOfMonthField) {     
  117.         if (dayOfMonthField == "?") {     
  118.             return true;     
  119.         }     
  120.     
  121.         if (dayOfMonthField.indexOf("L") >= 0) {     
  122.             return checkFieldWithLetter(dayOfMonthField, "L", 1, 7, -1, -1);     
  123.         } else if ( dayOfMonthField.indexOf("W") >= 0) {     
  124.             return checkFieldWithLetter(dayOfMonthField, "W", 1, 31, -1, -1);     
  125.         } else if (dayOfMonthField.indexOf("C") >= 0) {     
  126.             return checkFieldWithLetter(dayOfMonthField, "C", 1, 31, -1, -1);     
  127.         } else {     
  128.             return checkField( dayOfMonthField, 1, 31 );     
  129.         }     
  130.     }     
  131.     
  132.     
  133.     function checkMonthsField(monthsField) {     
  134. /*        monthsField = StringUtils.replace( monthsField, "JAN", "1" );   
  135.         monthsField = StringUtils.replace( monthsField, "FEB", "2" );   
  136.         monthsField = StringUtils.replace( monthsField, "MAR", "3" );   
  137.         monthsField = StringUtils.replace( monthsField, "APR", "4" );   
  138.         monthsField = StringUtils.replace( monthsField, "MAY", "5" );   
  139.         monthsField = StringUtils.replace( monthsField, "JUN", "6" );   
  140.         monthsField = StringUtils.replace( monthsField, "JUL", "7" );   
  141.         monthsField = StringUtils.replace( monthsField, "AUG", "8" );   
  142.         monthsField = StringUtils.replace( monthsField, "SEP", "9" );   
  143.         monthsField = StringUtils.replace( monthsField, "OCT", "10" );   
  144.         monthsField = StringUtils.replace( monthsField, "NOV", "11" );   
  145.         monthsField = StringUtils.replace( monthsField, "DEC", "12" );*/    
  146.     
  147.         monthsField.replace("JAN""1");     
  148.         monthsField.replace("FEB""2");     
  149.         monthsField.replace("MAR""3");     
  150.         monthsField.replace("APR""4");     
  151.         monthsField.replace("MAY""5");     
  152.         monthsField.replace("JUN""6");     
  153.         monthsField.replace("JUL""7");     
  154.         monthsField.replace("AUG""8");     
  155.         monthsField.replace("SEP""9");     
  156.         monthsField.replace("OCT""10");     
  157.         monthsField.replace("NOV""11");     
  158.         monthsField.replace("DEC""12");     
  159.     
  160.         return checkField(monthsField, 1, 31);     
  161.     }     
  162.     
  163.     function checkDayOfWeekField(dayOfWeekField) {     
  164. /*        dayOfWeekField = StringUtils.replace( dayOfWeekField, "SUN", "1" );   
  165.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "MON", "2" );   
  166.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "TUE", "3" );   
  167.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "WED", "4" );   
  168.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "THU", "5" );   
  169.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "FRI", "6" );   
  170.         dayOfWeekField = StringUtils.replace( dayOfWeekField, "SAT", "7" );*/    
  171.     
  172.         dayOfWeekField.replace("SUN""1" );     
  173.         dayOfWeekField.replace("MON""2" );     
  174.         dayOfWeekField.replace("TUE""3" );     
  175.         dayOfWeekField.replace("WED""4" );     
  176.         dayOfWeekField.replace("THU""5" );     
  177.         dayOfWeekField.replace("FRI""6" );     
  178.         dayOfWeekField.replace("SAT""7" );             
  179.     
  180.         if (dayOfWeekField == "?") {     
  181.             return true;     
  182.         }     
  183.     
  184.         if (dayOfWeekField.indexOf("L") >= 0) {     
  185.             return checkFieldWithLetter(dayOfWeekField, "L", 1, 7, -1, -1);     
  186.         } else if (dayOfWeekField.indexOf("C") >= 0) {     
  187.             return checkFieldWithLetter(dayOfWeekField, "C", 1, 7, -1, -1);     
  188.         } else if (dayOfWeekField.indexOf("#") >= 0) {     
  189.             return checkFieldWithLetter(dayOfWeekField, "#", 1, 7, 1, 5);     
  190.         } else {     
  191.             return checkField(dayOfWeekField, 1, 7);     
  192.         }     
  193.     }     
  194.     
  195.     function checkYearField(yearField) {     
  196.         return checkField(yearField, 1970, 2099);     
  197.     }     
  198.     
  199.     
  200.     function checkFieldWithLetter(value, letter, minimalBefore, maximalBefore,     
  201.                                           minimalAfter, maximalAfter) {     
  202.         var canBeAlone = false;     
  203.         var canHaveIntBefore = false;     
  204.         var canHaveIntAfter = false;     
  205.         var mustHaveIntBefore = false;     
  206.         var mustHaveIntAfter = false;     
  207.     
  208.         if (letter == "L") {     
  209.             canBeAlone = true;     
  210.             canHaveIntBefore = true;     
  211.             canHaveIntAfter = false;     
  212.             mustHaveIntBefore = false;     
  213.             mustHaveIntAfter = false;     
  214.         }     
  215.         if (letter == "W" || letter == "C") {     
  216.             canBeAlone = false;     
  217.             canHaveIntBefore = true;     
  218.             canHaveIntAfter = false;     
  219.             mustHaveIntBefore = true;     
  220.             mustHaveIntAfter = false;     
  221.         }     
  222.         if (letter == "#") {     
  223.             canBeAlone = false;     
  224.             canHaveIntBefore = true;     
  225.             canHaveIntAfter = true;     
  226.             mustHaveIntBefore = true;     
  227.             mustHaveIntAfter = true;     
  228.         }     
  229.     
  230.         var beforeLetter = "";     
  231.         var afterLetter = "";     
  232.     
  233.         if (value.indexOf(letter) >= 0 ) {     
  234.             beforeLetter = value.substring( 0, value.indexOf(letter));     
  235.         }     
  236.     
  237.         if (!value.endsWith(letter)) {     
  238.             afterLetter = value.substring( value.indexOf( letter ) + 1 );     
  239.         }     
  240.     
  241.         if (value.indexOf(letter) >= 0) {     
  242.             if (letter == value) {     
  243.                 return canBeAlone;     
  244.             }     
  245.     
  246.             if (canHaveIntBefore) {     
  247.                 if (mustHaveIntBefore && beforeLetter.length == 0) {     
  248.                     return false;     
  249.                 }     
  250.     
  251.                 if (!checkIntValue(beforeLetter, minimalBefore, maximalBefore, true)){     
  252.                     return false;     
  253.                 }     
  254.             } else {     
  255.                 if (beforeLetter.length > 0 ) {     
  256.                     return false;     
  257.                 }     
  258.             }     
  259.     
  260.             if (canHaveIntAfter) {     
  261.                 if ( mustHaveIntAfter && afterLetter.length == 0 ) {     
  262.                     return false;     
  263.                 }     
  264.     
  265.                 if (!checkIntValue(afterLetter, minimalAfter, maximalAfter, true)) {     
  266.                     return false;     
  267.                 }     
  268.             } else {     
  269.                 if (afterLetter.length > 0) {     
  270.                     return false;     
  271.                 }     
  272.             }     
  273.         }     
  274.     
  275.         return true;     
  276.     }     
  277.     
  278. /*    function checkIntValue(value, minimal, maximal) {   
  279.         return checkIntValue(value, minimal, maximal, true);   
  280.     } */    
  281.     
  282.     function checkIncrementField(value, minimal, maximal) {     
  283.         var start = value.substring(0, value.indexOf("/"));     
  284.     
  285.         var increment = value.substring(value.indexOf("/") + 1);     
  286.     
  287.         if (!("*" == start)) {     
  288.             return checkIntValue(start, minimal, maximal, true) && checkIntValue(increment, minimal, maximal, false);     
  289.         } else {     
  290.             return checkIntValue(increment, minimal, maximal, true);     
  291.         }     
  292.     }     
  293.     
  294.     
  295.     
  296.     function checkListField(value, minimal, maximal ) {     
  297.         var st = value.split(",");     
  298.     
  299.         var values = new Array(st.length);     
  300.     
  301.         for(var j = 0; j < st.length; j++) {     
  302.             values[j] = st[j];     
  303.         }     
  304.     
  305.         var previousValue = -1;     
  306.     
  307.         for (var i= 0; i < values.length; i++) {     
  308.             var currentValue = values[i];     
  309.     
  310.             if (!checkIntValue(currentValue, minimal, maximal, true)) {     
  311.                 return false;     
  312.             }     
  313.     
  314.             try {     
  315.                 var val = parseInt(currentValue, 10);     
  316.     
  317.                 if (val <= previousValue) {     
  318.                     return false;     
  319.                 } else {     
  320.                     previousValue = val;     
  321.                 }     
  322.             } catch (e) {     
  323.                 // we have always an int     
  324.             }     
  325.         }     
  326.     
  327.         return true;     
  328.     }    

参考:http://blog.csdn.net/q421022/article/details/4504068

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值