Quartz Scheduler and CRON/Schedule
The scheduler uses schedule (CRON) expressions to configure instances of CronTrigger.
Format
Schedule expressions are strings that comprise seven sub-strings, or fields, that describe the schedule's details. These sub-strings are separated with a white-space, and represent:
|
Field Position |
Field Name |
Mandatory? |
Allowed Values |
Allowed Special Characters |
|---|---|---|---|---|
|
1 |
Seconds |
Yes |
0—59 |
/,—- |
|
2 |
Minutes |
Yes |
0—59 |
/,—- |
|
3 |
Hours |
Yes |
0—23 |
/,—- |
|
4 |
Day-of-month |
Yes |
1—31 |
/,—? L W - |
|
5 |
Month |
Yes |
1—12 or JAN—DEC |
/,—- |
|
6 |
Day-of-week |
Yes |
0—6 or SUN—SAT |
/,—? L # - |
|
7 |
Year |
No |
empty, 19702099 |
/, - |
Special Characters
These are the special characters that are supported by the Quartz Scheduler implementation (taken from http://en.wikipedia.org/wiki/Cron).
Asterisk—An asterisk (*) indicates that the schedule (CRON) expression matches for all values of the field. For example, using an asterisk in the fifth field (the month field) indicates every month.
Slash—A slash (/) describes increments of ranges. For example, 3-59/15 in the second field (the minutes field) indicate the third minute of the hour and every 15 minutes thereafter. The form “*/…” is equivalent to the form “first-last/…”, that is, an increment over the largest possible range of the field.
Comma—A comma (,) is used to separate items of a list. For example, using “MON,WED,FRI” in the sixth field (the Day-of-week field) means Mondays, Wednesdays and Fridays.
Hyphen—A hyphen (-) define ranges. For example, 2000-2010 indicates every year between 2000 and 2010 AD, inclusive.
L—‘L’ stands for last. When used in the Day-of-week field, it means the last day of the week (Sunday to Saturday means the seventh day or Saturday). If used in the Day-of-month field, it means the last day of the month. It is used to also specify constructs such as 'the last Friday' ('FRIL' or 6L) of a given month.
W—'W' stands for weekday. This character is allowed for the Day-of-month field and is used to specify the weekday (Monday-Friday) nearest the given day. For example:
- If the 15th is a Saturday, the trigger fires on Friday the 14th.
- If the 15th is a Sunday, the trigger fires on Monday the 16th.
- If the 15th is a Tuesday, then it fires on Tuesday the 15th.
Hash—A hash (#) is allowed for the Day-of-week field and must be followed by a number between 1 and 5. It is used to specify constructs such as 'the second Friday' of a given month (6#2 or FRI#2).
Question mark—A question mark (?) specifies no specific value. It allows a either the Day-of-month or Day-of-week field to be left empty.
Examples
|
Expression |
Schedule |
|---|---|
|
0 0 12 * * ? |
Fires at 12pm (noon) every day ‘?’ must be used in either Day-of-week or day-of-month. |
|
0 15 10 ? * * |
Fires at 10:15am every day. |
|
0 15 10 * * ? |
Fires at 10:15am every day. |
|
0 15 10 * * ? * |
Fires at 10:15am every day. |
|
0 15 10 * * ? 2014 |
Fires at 10:15am every day during the year 2014. |
|
0 * 14 * * ? |
Fires every minute starting at 2pm and ending at 2:59pm, every day. |
|
0 0/5 14 * * ? |
Fires every 5 minutes starting at 2pm and ending at 2:55pm, every day. |
|
0 0/5 14,18 * * ? |
Fires every 5 minutes starting at 2pm and ending at 2:55pm, AND every 5 minutes starting at 6pm and ending at 6:55pm, every day. |
|
0 0-5 14 * * ? |
Fires every minute starting at 2pm and ending at 2:05pm, every day. |
|
0 10,44 14 ? 3 WED |
Fires at 2:10pm and at 2:44pm every Wednesday in the month of March. |
|
0 15 10 ? * MON-FRI |
Fires at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday. |
|
0 15 10 15 * ? |
Fires at 10:15am on the 15th day of every month. |
|
0 15 10 L * ? |
Fires at 10:15am on the last day of every month. |
|
0 15 10 L-2 * ? |
Fires at 10:15am on the 2nd-to-last last day of every month. |
|
0 15 10 ? * 6L |
Fires at 10:15am on the last Friday of every month. |
|
0 15 10 ? * 6L |
Fires at 10:15am on the last Friday of every month. |
|
0 15 10 ? * 6L 2002-2005 |
Fires at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and Fires 2005. |
|
0 15 10 ? * 6#3 |
Fires at 10:15am on the third Friday of every month. |
|
0 0 12 1/5 * ? |
Fires at 12pm (noon) every 5 days every month, starting on the first day of the month. |
|
0 11 11 11 11 ? |
Fires every November 11th at 11:11am. |
