Transferred from: https://www.cnblogs.com/knowledgesea/p/4705796.html
Preface
Cron expression: is to use the simple xxoo symbol according to certain rules, you can put all kinds of time dimension expression incisively and vividly, everywhere, And then in the quart.net to do the task scheduling (timer service) in the cognitive implementation, can imagine this is how seamless, that is the coding art agglutination is not excessive.
After the blow, I'll write a few simple cron expressions to admire:
1, 0/20 * * * *? Note: every 20 seconds
2, 15 0/2 * * *?
Note: Every hour starts at 0 minutes, every 2 minutes 15th seconds, if the current time is 20:00:00, then his dimension is in: 20:02:5,20:04:15,20:06:15 ....
3, 0 0/2 17-21 * *?
Note: Every day from 5 o'clock in the afternoon to 9 o'clock, every 2 minutes in the No. 00 second, if the current time is 20:24:52, then his time dimension in: 20:26:00,20:28:00,20:30:00 ...
Seeing the simplicity and power of cron expressions, I can't help but Go down
The symbol and format table of a cron expression
Table 1, cron expression special character meaning schematic table
Special characters |
Significance |
* |
Match all the values. such as: * In the field of minutes to indicate every minute |
? |
Used only in date and weekday fields. It is used to specify a "non-explicit value" |
- |
Specifies a range. such as: "10-12" in the hour field means "10 points, 11 points, 12 points" |
, |
Specify several optional values. such as: "Mon,wed,fri" in the field of the week to say "Monday, Wednesday, Friday" |
/ |
Specifies the increment. such as: "0/15" in the second field means no minutes of 0,15,30 and 45 seconds. "5/15" in the minute field represents the 5,20,35 and 50 of the hour. The symbol "*" in front of "/" (eg: */10) equivalent to 0 in front of "/" (e.g. 0/10) |
L |
Represents the Day-of-month and Day-of-week fields, but has different meanings in two fields, such as the last day of one months in a day-of-month field. If the Day-of-week field represents ' 7 ' or ' SAT ', if preceded by a number in the Day-of-week field, it represents the last days of one months, e.g. ' 6L ' represents the last Friday of one months |
W |
Only date fields are allowed to appear. This character is used for the last working day of the specified date. For example, if you write "15W" in the Date field, it means: the most recent working day of 15th this month. So, if number 15th is Saturday, then the task will be triggered at number 14th. If the 15 good is Sunday, then the task will be triggered in Monday, the number 16th. If you fill in the Date field "1W" even if the number 1th is Saturday, then the task will only be in the next Monday, that is, the number 3rd trigger, the "W" character specified in the last working day is not able to cross the month. The character "W" can only be used with a single numeric value and cannot be a number field, such as: 1-15w is wrong |
Lw |
L and W can be used in conjunction with the Date field, LW indicates the last week of the month's working day |
# |
Only allowed to appear in the Week field. This character is used to specify a certain day of the month. For example: "6#3" means Friday of the third week of this month (6 for Friday, 3 for the third week). "2#1" represents the first week of this month in Monday. "4#5" means Wednesday of week fifth |
C |
Allowed to appear in date and weekday fields. This character relies on a specified "calendar". This means that the value of this expression depends on the results of the relevant "calendar", and if there is no calendar association, it is equivalent to all included calendars. For example: the Date field is "5C" for the first day of the associated calendar, or 5 days after the first day of the month. The week field is "1C" for the first day of the associated calendar, or 1 days after the first day of the week, that is, the day after Sunday (Monday) |
Table 2, Cron expression special character meaning corresponding table
Field |
Allowed values |
Allowed special characters |
Seconds |
0-59 |
, - * / |
Score of |
0-59 |
, - * / |
Hours |
0-23 |
, - * / |
Date within the month |
1-31 |
, - * ? /L W C |
Month |
1-12 or Jan-dec |
, - * / |
Week Date |
1-7 or Sun-sat |
, - * ? /L C # |
Year (optional) |
Leave Blank, 1970-2099 |
, - * / |
Cron Expression Special characters detailed
The next cron expression, I'll combine with quartz to describe. First, the cron expression is 7 fields, respectively: The second time of the moon and the anniversary, where the year is an optional type, that is, if he does not set the year of the case is the annual.
1. asterisk (*)
He can exist in every domain, contain all the valid values, and watch here is all that is included, meaning that all values on that field are represented on that field, added to the month, which means 1-12, that is, every month, will be executed.
Example 1:0**1*? Note: Every 1th hours of the month will be executed.
Example 2:0**? * * Note: 00 seconds per minute is executed.
2, question mark (? )
He must exist in the cron expression, there is only one, and he can only exist in the field of the day and week, that is, does not relate to the field is what value, optimistic, with * contains all values different, he is not related to the month of the day or the day of the week. If you define a specific value in the Week field, the day field must be a question mark (? ), if a specific value is defined in the day field, then the field must be a question mark (? )。 Why, because to avoid ambiguous expressions, such as the 20th day of March, is exactly the 5th day of the third week, then if in the day domain definition 20, the domain definition 5 So, the double definition, very egg hurts. Cron expressions are also not allowed.
Example 1:0 10 18? 3 WEB Note: Every Wednesday of March every year, 6:10 P.M. will be triggered
Example 2:0 10 18 15 3? Note: Every 15th day of March, 6:10 P.M. will be triggered.
3. Horizontal bar (-)
He's in a cron expression, there can be any domain, and if there is a field that represents the start and end time, he represents a time period.
Example 1:0 10 18 1-5 *? Note: Every month from 1th to 5th (including monthly numbers 1th and 5th, which will be triggered for a total of 5 days per month), 6:10 P.M. will be triggered
Example 2:0 10-15 *? * Note: The 10th minute of every hour to 15 minutes (including the 10th minute of every hour and 15 minutes, the total 5 minutes per hour will be triggered), will be triggered
4, comma (,)
In a cron expression, there can be any field, and if there is a field that represents an optional value, he is a multi-point concept.
Instance 1:10,20 * *? * Note: The 10th second of every minute and 20 seconds will be triggered
Example 2:0 10,20 *? Note: The 1th and 2nd days of each month are triggered by the 10th minute of the hour with the first 20 minutes.
5. Oblique Dash (/)
In a cron expression, there can be any field that represents the increment, which is the concept of setting a starting value in the domain, and then how much time each interval.
Instance 1:5/20 * *? * Note: The 5th second of every minute, 25 seconds, 45 seconds will be executed.
Example 2:0 * 2/2? * * Note: The 2nd hour of the day, 4 hours, 6 hours, 8th hour ... The 22nd hour of 00 minutes and 00 seconds will be triggered.
6. Pound sign (#)
In a cron expression, there can only be a field of weeks, which indicates the week of the week, and if it goes out of scope, it is ignored.
Instance 1:* * *? * 3#4 Note: Week 2 of the 4th Week of the month, triggered early in the morning.
Instance 2:* * *? * 6#2 Note: Week 5 of the 2nd week of the month, triggered early in the morning
Other characters, I do not do the example here, against the special symbol table, to understand it yourself. Thanks
Finally comes a tool for generating cron expressions: Http://pan.baidu.com/s/1pL7Y2uf
"Go" quartz.net time expression-----cron expression