Use Cron expression (reprint)

Reprinted from: https: //www.jianshu.com/p/e9ce1a7e1ed1

Cron expression format

Min {} {} {seconds hours date} {} {} {weeks Month Year {} (empty can)

Field allowance Allow special characters
second 0-59 , - * /
Minute 0-59 , - * /
hour 0-23 , - * /
date 1-31 , - * ? / L W C
month 1-12 JAN-DEC or , - * /
week 1-7 or SUN-SAT , - * ? / L C #
Of (can be empty) Blank, 1970-2099 , - * /

 

second

Allowed values ​​range: 0 ~ 59, a null value is not allowed, if the value is not valid, the scheduler will throw an exception SchedulerException

"*" Indicates a trigger every 1 second

"" On behalf of the task is triggered when the specified number of seconds triggers, such as "0,15,45" represents 0 seconds, 15 seconds and 45 seconds

"-" represents a range within the specified trigger, such as "25-45" for 25 seconds from the start trigger end trigger to 45 seconds, once every second trigger

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" is equivalent to "0"), the latter value represents an offset, such as "0/20" or "* / 20 ' Representative start from 0 seconds, 20 seconds, every 1 trigger, i.e. a trigger 1 0 seconds, 20 seconds once triggered, the trigger 40 seconds 1; "5/20" for 5 seconds trigger 1, a trigger 25 seconds 1, once the trigger 45 seconds; "10-45 / 20" stands for 20 seconds to hit in step [10, 45] within a time trigger point, i.e. once the trigger 10 seconds, 30 seconds trigger 1

minute

Allowed values ​​range: 0 ~ 59, a null value is not allowed, if the value is not valid, the scheduler will throw an exception SchedulerException

"*" Indicates a trigger every minute

"" Represents the trigger in the specified minutes, such as "20, 40" for 10 minutes, triggering 20 minutes and 40 minutes task

"-" represents a range within the specified trigger, such as "5-30" represents a start trigger from 5 minutes to 30 minutes end trigger, trigger every minute

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" is equivalent to "0"), the latter value represents an offset, such as "0/25" or "* / 25 ' Representative from start 0 minutes, every 25 minutes once a trigger, i.e. a trigger 1 0 minutes, 25 minutes trigger 1, the first 50 minutes once the trigger; "5/25" for 5 minutes once a trigger, the trigger 30 minutes 1, once the trigger 55 minutes; "10-45 / 20" represents the step-time point 20 minutes hit trigger [10, 45], the trigger 1 i.e. 10 minutes, 30 minutes trigger 1

hour

Allowed values ​​range: 0 ~ 23, a null value is not allowed, if the value is not valid, the scheduler will throw an exception SchedulerException

"*" Indicates a trigger every 1 hour

"" Represents the point in time specified trigger, such as "10,20,23" on behalf of 10 o'clock, 20 o'clock and 23 o'clock triggered task

"-" represents a specified period of time triggers, such as "20-23" represents the start trigger from 20:00 to 23:00 the end of the trigger, trigger every 1 hour

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" is equivalent to "0"), the latter value represents an offset, such as "0/1" or "* / 1" Representative from 0:00 start trigger, trigger once every 1 hour; "1/2" on behalf of the 1:00 start trigger, after the trigger once every 2 hours

date

Allowed values ​​range: 1 ~ 12 (JAN-DEC), a null value is not allowed, if the value is not valid, the scheduler will throw an exception SchedulerException

"*" Indicates a trigger every month

"" Represents the trigger on the month, such as "1,6,12" for January, June and December triggered task

"-" represents a month within the specified range trigger, such as "1-6" represents the start trigger from January to June, the end of the trigger, trigger every month

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" as "1"), the latter value represents an offset, such as "1/2" or "* / 2" Representative from January start trigger, two months once every trigger; "6/6" from the beginning of June on behalf of the trigger, triggered once every six months later; "1-6 / 12" expression means that each year 1 trigger month

week

Allowed values ​​range: 1 ~ 7 (SUN-SAT), 1 for Sunday (the first day of the week), and so on, as Saturday 7 (the last day of week), NOT NULL value, if the value is not valid the scheduler will throw an exception SchedulerException

"*" Represents every week triggered;

"?" And {date} are mutually exclusive, which means that if explicitly specified trigger {DATE}, then {} pointless week, so as not to cause conflict and confusion

"" Trigger agreement on behalf of the specified day of the week, such as "1,3,5" for Sunday, Tuesday and Thursday trigger

"-" represents the week within a specified range trigger, such as "2-4" for Monday start trigger to end trigger on Wednesday, triggering every other day

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" as "1"), the latter value represents an offset, such as "1/3" or "* / 3" Representative start trigger from Sunday, once every three days trigger; "1-5 / 2" in a range means that the expression [1,5], triggered every 2 days, i.e., Sunday, Tuesday, Thursday trigger

"L" if {} placeholder weeks if "L", i.e., the last day of the week of the trigger means, the trigger Saturday i.e., L = 7, or L = SAT, therefore, "5L" means the last month Thursday trigger

"#" Is used to specify a particular number of weeks, "#" front, day of the week, "#" represents the first few weeks later in the month, such as "2 # 2" for the second week of the month Monday, "5 # 3" It represents the third week of the month Thursday, therefore, "5L" this form is just "#" special formality

years

Allowed values ​​range: 1970 to 2099, can be null, if the value is not valid, the scheduler will throw an exception SchedulerException

"*" Represents all trigger year

"" Represents the only trigger in the specified year, such as "2011,2012,2013" for 2011, 2012 and 2013 triggered task

"-" stands within the specified range of years triggers, such as "2011-2020" represents From 2011 to the end of 2020 triggered the trigger, trigger every year

"/" Representative trigger step (STEP), "/" represents the initial value of the preceding value ( "*" as "1970"), the latter value represents an offset, such as "2011/2" or "* / 2" from 2011 on behalf of the trigger, triggered once every 2 years

"?" Note: In addition to {date} and {week} can be used to implement mutual exclusion, express meaningless information, other placeholders must have a specific meaning time and dependencies: year -> month - > date (week) -> hours -> minutes -> The number of seconds

Special characters

“*”

"*" Character is used to specify all values. Such as: "*" in the field domain in minutes that "every minute."

“?”

"?" Character used only in the date field and domain week. It is used to specify "non-explicit value." When you need to specify something in one of these two domains when it is useful. See the examples below you will understand. Date and day of the month in the date mutually exclusive when these two elements together should be set by a question mark to indicate not want to set that field.

“-”

"-" character is used to specify a range. Such as: "10-12" in the hour field means "10 o'clock, 11 o'clock, 12 o'clock."

“,”

"," Character is used to specify another value. Such as: "MON, WED, FRI" means "Monday, Wednesday, Friday" in the weeks domain.

“/”

"/" Character is used to specify increments. Such as: "0/15" means that the second domain 15, 30 and 45 seconds per minute. "5/15" in the minutes field indicates hour 5,20,35 and 50. The symbol "*" in the "/" in front (such as: * / 10) is equivalent to 0 in the "/" in front (eg: 0/10). Remember that an essentially: domain representation of each value is a maximum value and the minimum value set, such as: set the second domain and the domain is 0-59 minutes, 1-31 date field, the domain is 1 month 12. "/" Character can help you take the appropriate value in each character field. Such as: "7/6" in the time domain month of July, when only time will trigger, does not mean every June.

“L”

L is the 'last' is omitted wording may indicate day-of-month and day-of-week field, but have different meanings in the two fields, for example, day-of-month field represents the last day of the month. If the day-of-week field indicates '7' or 'the SAT', if the front of-week day-domain plus the number, it indicates the last day of the month, for example, '6L' means the last month a Friday.

“W”

Character "W" only allows the date fields appear. This character is used to specify the date of the most recent days. For example: If you write in the date field "15W", he said: this month the most recent 15 days. So, if the 15th is Saturday, the task will be triggered at No. 14. 15 If the good is Sunday, the task is triggered at No. 16 on Monday. If it is to fill in "1W" in the date field, even if No. 1 is a Saturday, then the task will only next Monday, which is the No. 3 trigger, "W" characters specified in recent days is not able to cross the month. Character "W" can only be used with a separate value can not be a number field, such as: 1-15W wrong.

"L" and "W" can be combined in a date field, LW represents the working day in the last week of this month.

“#”

"#" Character is allowed only in the week field. This character is used to specify a certain day of the month. For example: "6 # 3" represents the third week of this month on Friday (6 shows Friday, 3 represents the third week). "2 # 1" in the first week of the month on Monday. "4 # 5" indicates the fifth week Wednesday.

“C”

Character "C" in the date field and allows domain week appears. This character relies on a designated "Calendar." That depends on the value of the expression related to "Calendar" in the results, if not associated with "Calendar", this is equivalent to all the "Calendar" contains. Such as: the date field is "5C" represents the five days of the first day, or after the first day of the beginning of this month associated with "Calendar" in the. Week domain is "1C" represents the first day of the association "Calendar" in the first days or weeks after one day, that is, the day after Sunday's (Monday).

For example the expression

"0012 * *?" 12:00 every day trigger

"01510? * *" Trigger every day at 10:15 am

"01510 * *?" Trigger every day at 10:15 am

"01510 * *? *" Trigger every day at 10:15 am

"01510 * *? 2005" in 2005 triggered daily at 10:15 am

"0 * 14 * *?" During the 2:00 pm to 2:59 pm every day of every 1 minute when the

Every 5 minutes triggering "0 0/5 14 * *?" During a day at 2:00 pm to 2:55 pm

Every 5 minutes triggering "0 0/5 * 14, 18 *?" And 6:00 to 6:55 pm during the period of 2:00 to 2:55 every afternoon

Every 1 minute when the "00-514 * *?" During a day at 2:00 pm to 2:05 pm

"0 10,44 14? ​​3 WED" March of the year Wednesday afternoon 2:10 and 2:44 trigger

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

"0151015 *?" Trigger 15th of each month at 10:15 am

"0 15 10 L *?" On the last day of each month at 10:15 am triggering

"0 15 10? * 6L" the last Friday of each month at 10:15 am triggering

"0 15 10? * 6L 2002-2005" 2002 years to 2005, the last Friday of each month at 10:15 am triggering

"01510? * 6 # 3" is the third Friday of each month at 10:15 am triggering

Guess you like

Origin www.cnblogs.com/jayhou/p/10053357.html