Using the cron expression format
A cron expression is a string composed of six or seven fields separated by white space. This table lists the six required fields and one optional field:
Field name | Allowed values | Allowed special characters |
---|---|---|
Seconds | 0-59 | , - * / |
Minutes | 0-59 | , - * / |
Hours | 0-23 | , - * / |
Day-of-month | 1-31 | , - * ? / L W C |
Month | 1-12 or JAN-DEC | , - * / |
Day-of-week | 1-7 or SUN-SAT | , - * ? / L C # |
Year (Optional) | empty, 1970 onwards | , - * / |
This table describes special characters that are used in cron expressions:
Special character | Description |
---|---|
* | Used to specify all values. For example, * in the Minutes field means every minute. |
? | Used in the Day-of-month and Day-of-week fields. It is used to indicate no specific value. This is useful when you need to specify something in one of the two fields, but not the other. |
- | Used to specify ranges. For example, 10-12 in the Hours field means the hours 10, 11, and 12. |
, | Used to specify additional values. For example, MON, WED, FRI in the Day-of-week field means the days Monday, Wednesday, and Friday. |
/ | Used to specify increments. For example, 0/15 in the Seconds field means the seconds 0, 15, 30, and 45. Another example is 5/15 in the Seconds field means the seconds 5, 20, 35, and 50. You can also specify / after the * character. In this case, * is equivalent to having 0 before the /. |
L |
Used in the Day-of-month and Day-of-week fields. This character is a short-hand for "last," but it has a different meaning in each of the two fields. For example, the value L in the Day-of-month field means the last day of the month - day 31 for January, day 28 for February on non-leap years. If used in the Day-of-week field by itself, it simply means 7 or SAT. If L is used in the Day-of-week field after another value, it means the last xxx day of the month. For example, 6L means the last Friday of the month. When using the L option, it is important not to specify lists, or ranges of values, as you get confusing results. |
W |
Used in the Day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. For example, if you specify 15W as the value for the Day-of-month field, it means the nearest weekday to the 15th of the month. Therefore, if the 15th is a Saturday, then the job runs on Friday the 14th. If the 15th is a Sunday, then the job runs on Monday the 6th. If the 15th is a Tuesday, then it runs on Tuesday the 15th. If you specify 1W as the value in the Day-of-month field, and the first day is a Saturday, then the job runs on Monday the 3rd, as it does not jump over the boundary of a month's days. The W character is only specified when the day-of-month is a single day, not a range or list of days. |
L and W |
The L and W characters can also be combined for the day-of-month expression to yield LW, which translates to last weekday of the month. |
# | Used in the Day-of-week field. This character is used to specify the nth XXX day of the month. For example, the value of 6#3 in the Day-of-week field means the third Friday of the month (day 6 = Friday and "#3" = the 3rd one in the month). |
C | Used in the Day-of-month and Day-of-week fields. This character is
the indicator for calendar. This means values are calculated against the
associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of 5C in the Day-of-month field means the first day included by the calendar on or after the 5th. A value of 1C in the Day-of-week field means the first day included by the calendar on or after Sunday. |
The legal characters and the names of months and days of the week are not case sensitive.
This table lists some examples:
Expression | Meaning |
---|---|
0 0 12 * * ? | Run at 12 PM (noon) every day. |
0 15 10? * * | Run at 10:15 AM every day. |
0 15 10 * * ? | Run at 10:15 AM every day. |
0 15 10 * * ? * | Run at 10:15 AM every day. |
0 15 10 * * ? 2005 | Run at 10:15 AM every day during the year 2005. |
0 * 14 * * ? | Run every minute starting at 2 PM and ending at 2:59 PM, every day. |
0 0/5 14 * * ? | Run every 5 minutes starting at 2 PM and ending at 2:55 PM, every day. |
0 0/5 14,18 * * ? | Run every 5 minutes starting at 2 PM and ending at 2:55 PM, and run every 5 minutes starting at 6 PM and ending at 6:55 PM, every day. |
0 0-5 14 * * ? | Run every minute starting at 2 PM and ending at 2:05 PM, every day. |
0 10,44 14 ? 3 WED | Run at 2:10 PM and at 2:44 PM every Wednesday in the month of March. |
0 15 10 ? * MON-FRI | Run at 10:15 AM every Monday, Tuesday, Wednesday, Thursday, and Friday. |
0 15 10 15 * ? | Run at 10:15 AM on the 15th day of every month. |
0 15 10 L * ? | Run at 10:15 AM on the last day of every month. |
0 15 10 ? * 6L | Run at 10:15 AM on the last Friday of every month. |
0 15 10 ? * 6L 2002-2005 | Run at 10:15 AM on every last Friday of every month during the years 2002, 2003, 2004 and 2005. |
0 15 10 ? * 6#3 | Run at 10:15 AM on the third Friday of every month. |
0 30 3 * * ? | Run at 3:30 PM every day. |