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:
(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
1. Structure
corn left to right (separated by spaces): second minute hour month day month day day year
Second, the meaning of each field
field | allowance | allowed special characters |
Seconds | Integer from 0 to 59 | , - * / four characters |
分(Minutes) | Integer from 0 to 59 | , - * / four characters |
Hours ( Hours ) | Integer from 0 to 23 | , - * / four characters |
date ( DayofMonth ) | Integer from 1 to 31 (but you need to consider the number of days in your month) | ,- * ? / LWC eight characters |
month ( Month ) | Integer from 1 to 12 or JAN-DEC | , - * / four characters |
DayofWeek _ _ | Integer from 1 to 7 or SUN-SAT (1=SUN) | , - * ? / LC # eight characters |
year (optional, leave blank) ( Year ) | 1970~2099 | , - * / four characters |
Precautions:
Each field uses numbers, but the following special characters can also appear, their meanings are:
(1)*: Indicates any value that matches the field. If you use * in the Minutes field, it means that the event will be triggered every minute.
(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.
(3)-: Indicates the range. For example, use 5-20 in the Minutes field, which means that it is triggered every minute from 5 minutes to 20 minutes.
(4)/: Indicates that the trigger starts at the start time, and then triggers every fixed time. For example, using 5/20 in the Minutes field means that it is triggered once every 5 minutes, and once every 25, 45, etc.
(5),: Indicates that enumeration values are listed. For example: use 5,20 in the Minutes field, it means to fire every minute at 5 and 20 minutes.
(6) L: Indicates the last, which can only appear in the DayofWeek and DayofMonth fields. If you use 5L in the DayofWeek field, it means triggering on the last Thursday.
(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.
(8) LW: These two characters can be used together to indicate the last working day of a month, that is, the last Friday.
(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.
3. Examples of common expressions
(1) 0 0 2 1 * ? * means to adjust the task at 2 am on the 1st of each month
(2) 0 15 10 ? * MON-FRI means that the job is executed at 10:15 am every day from Monday to Friday
(3) 0 15 10 ? 6L 2002-2006 means that the operation will be executed at 10:15 am on the last Friday of each month in 2002-2006
(4) 0 0 10, 14, 16 ** ? 10am, 2pm, 4pm daily
(5) 0 0/30 9-17 * * ? every half hour during 9 to 5 working hours
(6) 0 0 12 ? * WED means every Wednesday at 12 noon
(7) 0 0 12 * * ? Triggered at 12 noon every day
(8) 0 15 10 ? * * Triggered every day at 10:15am
(9) 0 15 10 * * ? Triggered at 10:15 am every day
(10) 0 15 10 * * ? * Triggered every day at 10:15am
(11) 0 15 10 * * ? 2005 Fired at 10:15 am every day in 2005
(12) 0 * 14 * * ? fires every 1 minute between 2pm and 2:59pm every day
(13) 0 0/5 14 * * ? fires every 5 minutes between 2pm and 2:55pm daily
(14) 0 0/5 14,18 ** ? Fires every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
(15) 0 0-5 14 * * ? fires every 1 minute between 2pm and 2:05pm every day
(16) 0 10,44 14 ? 3 WED is triggered every March at 2:10pm and 2:44pm on Wednesdays
(17) 0 15 10 ? * MON-FRI triggers at 10:15 a.m. Monday-Friday
(18) 0 15 10 15 * ? Triggered at 10:15 am on the 15th of every month
(19) 0 15 10 L* ? Triggered at 10:15 am on the last day of the month
(20) 0 15 10 ? * 6L triggers on the last Friday of every month at 10:15 am
(21) 0 15 10 ? * 6L 2002-2005 Triggered at 10:15 am on the last Friday of every month from 2002 to 2005
(22) 0 15 10 ?* 6#3 Triggered at 10:15am on the third Friday of every month
Note:
(1) Some subexpressions can contain some 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 "3/20" in the subexpression (minutes)
every 15 minutes starting from the 0th minute
" means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")
The "?" character is only used for the day (month) and day (week) sub-expressions, indicating that no value is specified.
When one of the two sub-expressions is assigned a value, in order to avoid conflicts, the other sub-expression needs to be replaced. The value of the formula is 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) subexpression, "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" for the 6th last day of the month, "FRIL" for the last Friday of the month
Note: When using the "L" parameter, do not specify a list or range as this can cause problems
Reprinted from: https://www.cnblogs.com/javahr/p/8318728.html