Quartz-time expression-----Cron Expression Details _ timer

Source: Internet
Author: User
Tags numeric value
Preface

Cron expression: is to use a simple xxoo symbol according to a certain rule, you can put a variety of time dimensional expression of the incisively and vividly, everywhere, Then in the quart.net to do task scheduling (timed service) in the cognitive implementation, it can be imagined how seamless, that is coding art agglutination is not excessive.

After the blow, I'll write a few simple cron expressions to appreciate:

1, 0/20 * * * *? Note: every 20 seconds

2, 15 0/2 * * *?

Note: Every hour starting from 0 minutes, every 2 minutes of the 15th seconds, if the current time is 20:00:00, then his dimension is: 20:02:5,20:04:15,20:06:15 ...

3, 0 0/2 17-21 * *?

Note: Every day 5 o'clock in the afternoon to the night 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 ...

See the simplicity and power of a cron expression and go down deep, the sign of the go cron expression, the format table

Table 1, cron expression special character meaning schematic table

Special characters

Significance

*

Matches all of the values. such as: * In the field of the minutes to represent every minute

?

Used only in date fields and week fields. It is used to specify "ambiguous values"

-

Specifies a range. For example: "10-12" in the hour field means "10 points, 11 points, 12 points"

,

Specifies several optional values. such as: "Mon,wed,fri" in the week field, said "Monday, Wednesday, Friday"

/

Specifies the increment. For example: "0/15" in the second field means no minutes of 0,15,30 and 45 seconds. "5/15" indicates no hours of 5,20,35 and 50 in the minute field. The symbol "*" in front of "/" (e.g., */10) is equivalent to 0 before "/" (e.g. 0/10)

L

Represents the Day-of-month and Day-of-week fields, but in two fields the meaning is different, for example, 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 means the last days of one months, for example ' 6L ' means the last Friday of one months.

W

Only date fields are allowed to appear. This character is used for the most recent weekday of the specified date. For example: If you write "15W" in the date field, it says: The most recent working day for 15th this month. So, if number 15th is Saturday, then the task will be triggered at number 14th. If 15 is good for Sunday, then the task will be triggered in Monday, the number 16th. If you fill in the "1W" in the Date field, even if number 1th is Saturday, then the task will only be in the next Monday, that is, 3rd, the "W" character specified in the last weekday is not able to cross the month. The character "W" can only be used in conjunction with a single numeric value and cannot be a number segment, such as: 1-15w is wrong

Lw

L and W can be used jointly in a Date field, LW represents the last week of the month

#

Only allowed to appear in the week domain. 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, and 3 for the third week). "2#1" means Monday of the first week of this month. "4#5" means Wednesday of week fifth

C

Allowed to appear in date fields and week fields. This character is dependent on a specified "calendar". This means that the value of the expression depends on the result of the related calendar, and if there is no calendar association, it is equivalent to all the included calendars. For example, the Date field is "5C" that represents the first day of the associated "Calendar", or the first day after the beginning of this month. 5 days. The week field is "1C" indicating the first day of the associated calendar, or the 1 days after the first day of the week, the day after Sunday (Monday)

Table 2, Cron expression special character meaning corresponding table

Field

Allowed values

Special characters that are allowed

Seconds

0-59

, - * /

Part

0-59

, - * /

Hours

0-23

, - * /

Date within the month

1-31

, - * ? /L W C

Month

1-12 or Jan-dec.

, - * /

Date in the week

1-7 or Sun-sat.

, - * ? /L C #

Years (optional)

Leave Blank, 1970-2099

, - * /

Cron Expression Special characters detailed

The next cron expression, I'll describe it in conjunction with quartz. First, the CRON expression has 7 domains, in order: Second time, the year is the optional type, that is, if he does not set the annual score is every year.

1, asterisk (*)

He can exist in every field, contains all the legal values, and watch that this is all inclusive, meaning that all the values on that field are represented on that field, added in the month, which means 1-12, that is, every month, will be executed.

Example 1:0**1*. Note: Every month, number 1th will be carried out early morning.

Example 2:0**. * * Note: 00 seconds per minute is executed.

2, question mark (. )

He's in a cron expression, there must be, there is only one, and he can only exist in the fields of day and week, which means that it does not matter what the value of the field is, and that it is different from the containing value of the *, that he is not related to the day of the month 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 you define a specific value in the Day field, the week field must be a question mark (. )。 Why, because avoiding ambiguous expressions, such as the 20th day of March, is just the 5th day of the third week, if you define 20 in the day domain, and the definition of the week field is 5, it's a double definition. Cron expressions are also not allowed.

Example 1:0 10 18? 3 WEB Note: Every Wednesday of every March, 6:10 P.M. will be triggered.

Example 2:0 10 18 15 3? Note: The 15th day of March every year, 6:10 P.M. will be triggered

3, horizontal bar (-)

He can have any field in a cron expression, and if there is a field that represents the start and end time, he represents a time period.

Instance 1:0 10 18 1-5 *. Note: 1th to 5th per month (including 1th and 5th per month, 5 days a month will be triggered), 6:10 P.M. will be triggered

Example 2:0 10-15 *. * * Note: the 10th minute of every hour to the first 15 minutes (including the 10th minute and 15 minutes per hour, the total of 5 minutes per hour will be triggered), will be triggered

4, comma (,)

He can have any field in a cron expression, and if there is a field that represents an optional value, he is a more point concept.

Instance 1:10,20 * *? * * Note: Every minute of the 10th second and the first 20 seconds will be triggered

Example 2:0 10,20 * 1,2 *? Note: The 1th day of the month and the 2nd day, the 10th minute of each hour and the first 20 minutes are triggered.

5, Oblique line (/)

In a cron expression, he can have any field that represents an increment, which is the concept of setting a starting value in a field, and then how many times each time.

Instance 1:5/20 * *? * * Note: The 5th second of every minute, the first 25 seconds, the first 45 seconds will be executed.

Example 2:0 * 2/2? * * Note: The 2nd hour of the day, the first 4 hours, the first 6 hours, the 8th hour ... The 22nd hour of 00 minutes and 00 seconds will be triggered.

6, Well No. (#)

He's in a cron expression, you can only have the week field, which means the week of the week, and if it's out of range, ignore it.

Instance 1:* * *? * 3#4 Note: Week 2 of the 4th week of each month, early morning trigger.

Instance 2:* * *? * 6#2 Note: Week 5 of the 2nd week of each month, early morning trigger

Other characters, I do not do the example here, against the special symbol table, you understand it. Thanks

Finally comes with a tool to generate a cron expression: Http://pan.baidu.com/s/1pL7Y2uf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.