Detailed cron expression

Cron expressions are mainly used in the Quartz framework. Quartz is an open-source job scheduling framework written entirely in java. Its main function is the scheduler (complete scheduled tasks), which can be combined with javaEE or javaSE applications or used alone. , can support clustering and load balancing with the help of relational database and JDBC job storage.

Tools/Materials

  • quartz-all-1.6.0.jar
  • spring-context-support.jar
  • commons-collections-3.2.jar

method/step

  1. 1

    A Cron expression is a string. The string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats: 

    Seconds Minutes Hours DayofMonth Month DayofWeek Year或 Seconds Minutes Hours DayofMonth Month DayofWeek

  2. 2

    The characters that can appear in each field are as follows: 

    Seconds: Four characters ", - * /" can appear, and the valid range is an integer from 0 to 59. 

    Minutes: Four characters ", - * /" can appear, and the valid range is an integer from 0 to 59. 

    Hours: Four characters ", - * /" can appear, and the valid range is an integer from 0 to 23. 

    DayofMonth: Eight characters of ", - * / ? LW C" can appear, and the valid range is an integer from 0 to 31. 

    Month: Four characters ", - * /" can appear, the valid range is an integer from 1-12 or JAN-DEc 

    DayofWeek: Four characters of ", - * / ? LC #" can appear, and the valid range is an integer of 1-7 or two ranges of SUN-SAT. 1 for Sunday, 2 for Monday, and so on 

    Year: Four characters of ", - * /" can appear, and the valid range is 1970-2099

    Each field uses numbers, but the following special characters can also appear, their meanings are:

  3. 3

    (1)*: Indicates any value that matches this field. If you use * in the Minutes field, it means that an event will be triggered every minute.

  4. 4

    (2)?: It can only be used in the fields of DayofMonth and DayofWeek. It also matches arbitrary values ​​of the domain, but doesn't actually. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger the scheduling on the 20th of each month, no matter what day of the week the 20th is, you can only use the following notation: 13 13 15 20 * ?, where the last digit can only be used? , and cannot use *, if you use * to indicate that it will trigger regardless of the day of the week, which is not the case.

  5. 5

    (3)-: Indicates the range, for example, 5-20 is used in the Minutes field, which means that it is triggered every minute from 5 minutes to 20 minutes.

  6.  

    (4)/: Indicates that the trigger starts at the start time, and then triggers every fixed time. For example, if 5/20 is used in the Minutes field, it means that it is triggered once every 5 minutes, and once at 25, 45, etc., respectively.

  7.  

    (5),: Indicates that the enumeration value is listed. For example: use 5,20 in the Minutes field, it means to fire every minute at 5 and 20 minutes.

  8.  

    (6) L: Indicates the last, which can only appear in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it means that it is triggered on the last Thursday.

  9.  

    (7) W: Indicates a valid working day (Monday to Friday), which can only appear in the DayofMonth field, and the system will trigger the event on the valid working day closest to the specified date. For example: use 5W on DayofMonth, if the 5th is Saturday, it will trigger on the nearest weekday: Friday, which is the 4th. If the 5th is Sunday, it will trigger on the 6th (Monday); if the 5th is one of Monday to Friday, it will trigger on the 5th. Another point, W's most recent searches don't span months.

  10.  

    (8) LW: These two characters can be used together to indicate the last working day of a month, that is, the last Friday. 

  11.  

    (9)#: Used to determine the day of the week in each month, which can only appear in the DayofMonth field. For example, in 4#2, it means the second Wednesday of a month.

  12.  

    To give a few examples: 

    0 0 2 1 * ? * means to schedule the task at 2 am on the 1st of each month 

    0 15 10 ? * MON-FRI means execute the job at 10:15am every day from Monday to Friday 

    0 15 10 ? 6L 2002-2006 Indicates that the operation is executed at 10:15 am on the last Friday of each month in 2002-2006.

  13.  

    A cron expression has at least 6 (and possibly 7) time elements separated by spaces. 

    in order of 

    Seconds (0~59) 

    minutes (0~59) 

    hour (0~23) 

    Day (month) (0~31, but you need to consider the number of days in your month) 

    Month (0~11) 

    Day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT) 

    Year (1970-2099

  14.  

    where each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a list (1,3,5) , wildcard. Since the two elements "day of the month" and "day of the week" are mutually exclusive, must one be set?

    0 0 10, 14, 16 * * ? 10am, 2pm, 4pm daily 

    0 0/30 9-17 * * ? every half hour during 9 to 5 working hours 

    0 0 12 ? * WED means every Wednesday at 12 noon 

    "0 0 12 * * ?" fires at 12 noon every day 

    "0 15 10 ? * *" fires at 10:15 am every day 

    "0 15 10 * * ?" fires every day at 10:15 am 

    "0 15 10 * * ? *" fires at 10:15 am every day 

    "0 15 10 * * ? 2005" fires at 10:15 am every day in 2005 

    "0 * 14 * * ?" fires every 1 minute between 2pm and 2:59pm every day 

    "0 0/5 14 * * ?" fires every 5 minutes between 2pm and 2:55pm every day 

    "0 0/5 14,18 * * ?" fires every 5 minutes from 2pm to 2:55pm and from 6pm to 6:55pm every day 

    "0 0-5 14 * * ?" fires every 1 minute between 2pm and 2:05pm every day 

    "0 10,44 14 ? 3 WED" fires every March Wednesday at 2:10pm and 2:44pm 

    "0 15 10 ? * MON-FRI" fires at 10:15 am Monday to Friday 

    "0 15 10 15 * ?" fires at 10:15 am on the 15th of every month 

    "0 15 10 L * ?" Fires at 10:15 am on the last day of every month 

    "0 15 10 ? * 6L" fires at 10:15 am on the last Friday of every month 

    "0 15 10 ? * 6L 2002-2005" Fires at 10:15 am on the last Friday of every month from 2002 to 2005 

    "0 15 10 ? * 6#3" fires at 10:15 am on the third Friday of every month

  15.  

    Some subexpressions can contain ranges or lists.

    For example: subexpression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED,SAT"

    The "*" character represents all possible values

    Therefore, "*" in the subexpression (month) means the meaning of each month, and "*" in the subexpression (day (week)) means each day of the week

     

    The "/" character is used to specify the increment of the value 

    For example: "0/15" in the subexpression (minutes) means starting from the 0th minute, every 15 minutes 

    "3/20" in the subexpression (minutes) means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")

     

    The "?" character is only used for day (month) and day (week) subexpressions, indicating that no value is specified 

    When one of the two subexpressions is assigned a value, in order to avoid conflicts, the value of the other subexpression needs to be set to "?"

    The "L" character is only used in day (month) and day (week) subexpressions, it is an abbreviation for the word "last" 

    But its meaning is different in the two subexpressions. 

    In the day (month) subexpression, "L" represents the last day of the month 

    In the day (week) self-expression, "L" represents the last day of the week, which is SAT

    If there is specific content before the "L", it has other meanings

    For example: "6L" means the 6th last day of the month, "FRIL" means the last Friday of the month 

    Note: When using the "L" parameter, do not specify a list or range as this can cause problems.

    END

Precautions

  • Field Allowed Values ​​Allowed special characters:
  • seconds 0-59 , -*/
  • points 0-59 , -*/
  • hours 0-23 , -*/
  • Dates 1-31, -*?/LWC
  • Month 1-12 or JAN-DEC , - * /
  • Week 1-7 or SUN-SAT , -* ? / LC #
  • year (optional) leave blank, 1970-2099 , - * /

 

Quoting the original text: https://jingyan.baidu.com/article/7f41ecec0d0724593d095c19.html

 

Writing a blog is to remember things that I easily forget, and it is also a summary of my work. Articles can be reproduced without copyright. I hope to do my best to be better, and everyone to work hard and make progress together!

If there is any problem, welcome everyone to discuss it together. If there is a problem with the code, please correct me!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325116944&siteId=291194637