Cron Expression
Cron expressions are used to configure instances of CronTrigger class. A Cron expression is a string consisting of six or seven fields separated by white space that represents a set of times, normally as a schedule to execute some routine.
Below list the allowed fields and values.
Name | Mandatory | Constraints | Special Characters |
---|---|---|---|
Seconds | Y | 0 - 59 | , - * / |
Minutes | Y | 0 - 59 | , - * / |
Hours | Y | 0 - 23 | , - * / |
Day of month | Y | 1 - 31 | , - * ? / L W |
Month | Y | 0 - 11 or JAN - DEC | , - * / |
Day of week | Y | 1 - 7 or SUN - SAT | , - * ? / L # |
Year | N | empty or 1970 - 2099 | , - * / |
The month and weekday abbreviations are not case-sensitive.
The special characters are used as follow:
Special Character | Description |
---|---|
Asterisk * |
Used as wildcard that represents “all”. An asterisk in the hour field is equivalent to every hour. |
Comma , |
Used to separate items of a list. E.g., using MON,WED,FRI in the fifth field (day of week) means Mondays, Wednesdays and Fridays. |
Dash - |
Used to define ranges. E.g., using 2000-2010 indicates every year between 2000 and 2010, inclusive. |
Question Mark ? |
Used to specify “no specific value”. It is useful when you need to specify something in one of these two fields, but not in the other. For example, a trigger is to fire on the 10th of each month but it does not matter which day of the week, you can use ? in the day-of-week field. |
Slash / |
Used to skip a given number of values. E.g., */3 in the hour field means 0, 3 ,6, 9, 12, 15, 18, 21. The asterisk * specifies “every hour” but the /3 means only the first, fourth, seventh. etc. You can place a value before the slash to set initial value. E.g., 2/3 means 2, 5, 8, 11, etc. |
Hash # |
Used in day-of-week field to specify the “nth” xxx day of the month. Must be followed by a number between one and five. E.g., “5#3” in the day of week field corresponds to the third Friday of every month. |
Last L |
Used in day-of-month and day-of-week fields to specify either the last day of the month or the last xxx day of the month. In day-of-month field, L means the last day of the month, which is day 31 for January or 28 for February (non-leap years). In day-of-week field, L means 7 or SAT. If it is used in the day-of-week field after another value, e.g., 6L means the last Friday of the month. |
Weekday W |
Used in day-of-month field to specify the weekday of the month nearest to the given day. If 15W is specified in day-of-month field, it means the nearest weekday to the 15th of the month. So, if 15th is s Saturday, the trigger fires on Friday the 14th. If 15th is a Sunday, the trigger fires on Monday the 16th. If 15th is a Tuesday, it fires on Tuesday the 15th. However, if 1W is specified, and 1st is a Saturday, the trigger fires on Monday the 3rd, because it does not “jump” over the boundary of a month’s days. |
Some examples of Cron expressions.
Cron Expression | Description |
---|---|
0 0 12 * * ? |
Fire at 12:00 pm (noon) every day. |
0 15 10 ? * * |
Fire at 10:15 am every day. |
0 15 10 * * ? 2022 |
Fire at 10:15 am every day during the year 2022. |
0 * 14 * * ? |
Fire every minute starting at 2:00 pm and ending at 2:59 pm, every day. |
0 0/5 14,18 * * ? |
Fire every five minutes starting at 2:00 pm and ending at 2:55 pm, and fire every five minutes starting at 6:00 pm and ending at 6:55 pm, every day. |
0 10,44 14 ? 3 WED |
Fire 2:10 pm and at 2:44 pm, every Wednesday in the month of March. |
0 15 10 ? * 6#3 |
Fire at 10:15 am on the third Saturday of every month. |