Creating a Customized Schedule
For more advanced scheduling options, you can use a CronTrigger using a standard cron expression. A cron expression is a string consisting 6-7 fields (sub-expressions) that describe the details of the schedule. These fields are separated by a white space and are described in table Allowed Fields and Values of Cron Expressions in their expected order.
Field |
Required |
Allowed Values |
Allowed Special Characters |
Seconds |
Yes |
0-59 |
, - * / |
Minutes |
Yes |
0-59 |
, - * / |
Hours |
Yes |
0-23 |
, - * / |
Day of Month |
Yes |
1-3 |
, - * / ? L W |
Month |
Yes |
0-11 or JAN-DEC |
, - * / |
Day of Week |
Yes |
1-7 or SUN-SAT |
, - * / ? L # |
Year |
No |
empty or 1970-2099 |
, - * / |
The purpose of the special characters is explained in the following table:
Character |
Description |
, |
Used to specify a list of values not in a range. For example: “2,4,6” or “MON,WED,FRI” in the day-of-the-week field means “the days Monday, Wednesday and Friday”. |
- |
Used to indicate a range of values or an offset. For example “10-12” in the hour field means “the hours 10-12”. |
* |
Wildcard meaning every possible value of this field. |
/ |
Used to specify increments to values. For example “0/15” in the minutes field means “every 15th minute of the hour, starting at minute zero”, i.e. 0, 15, 30, and 45. A “3/20” in the minutes field means “every 20 minutes starting at minute 3”, i.e. 3, 23 and 43. A “1/3” in the day-of-the-month field means “every three days starting on the first day of the month.” Omitting the initial number is equivalent to using 0, i.e. “/35” is the same as “0/35”. |
? |
Used for day-of-the-month and day-of-the-week fields to mean “no specific value”. This is useful when you must specify something in one of the two fields but not the other. For example if you want to schedule a job for the 10th day of the month, regardless of the day of the week, you would put a “10” in the day-of-the-month field and a “?” in the day-of-the-week field. |
L |
Used for day-of-the-month and day-of-the-week fields and is used to specify “Last”, but it has different interpretations in each of those fields. An “L” in the day-of-the-month field means the “last day of the month, i.e. 31 for January or 28 for February on non-leap years and 29 on leap years. If used in the day-of-the-week field by itself, it means “7” or “SAT”, but if used in the day-of-the-week field after another value, it means the last X day of the month. For example, “6L” or “FRIL” both mean “the last Friday of the month.” You can also specify offsets such as “L-3” which means the third to the last day of the calendar month. Note:When using the “L” option, it is important not to specify lists or ranges of values as you will get unexpected results. The “L” and “W” characters can be combined in the day-of-the-month field to yield “LW” which means “last weekday of the month”. |
W |
Used to specify the “Weekday” (Monday-Friday) nearest the given day. For example 15W as a day-of-the-month value means, “the nearest weekday to the 15th of the month.” However, if you specify “1W” as the value for the day-of-the-month and the 1st is on a Saturday, the job is scheduled for Monday, the 3rd because the previous Friday (which is closer to the 1st) is in a different month. Note:When using the “W” option, it is important not to specify lists or ranges of values as you will get unexpected results. |
# |
Used for day-of-the-week fields to specify nth X day of the month. For example, the value “6#3” or “FRI#3” means “the third Friday of the month.” Note:If you specify #5 and there are not 5 of the given day-of-the-week in the month, then no firing occurs that month. |
Cron Expression Examples
The following table gives a list of example cron expressions.
Expression |
Meaning |
0 0 12 * * ? |
Fire at 12:00pm (noon) every day |
0 15 10 ? * * |
Fire at 10:15am every day |
0 15 10 * * ? |
Fire at 10:15am every day |
0 15 10 * * ? * |
Fire at 10:15am every day |
0 15 10 * * ? 2017 |
Fire at 10:15am every day during the year 2017 |
0 10,44 14 ? 3 WED |
Fire at 2:10pm and at 2:44pm every Wednesday in the month of March |
0 15 10 ? * MON-FRI |
Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday, and Friday |
0 15 10 15 * ? |
Fire at 10:15am on the 15th day of every month |
0 15 10 L * ? |
Fire at 10:15am on the last day of every month |
0 15 10 L-2 * ? |
Fire at 10:15am on the 2nd-to-last day of every month |
0 15 10 ? * 6L |
Fire at 10:15am on the last Friday of every month |
0 15 10 ? * 6L |
Fire at 10:15am on the last Friday of every month |
0 15 10 ? * 6L 2017-2020 |
Fire at 10:15am on every last Friday of every month during the years 2017, 2018, 2019, and 2020 |
0 15 10 ? * 6#3 |
Fire at 10:15am on the third Friday of every month |
0 0 12 1/5 * ? |
Fire at 12pm (noon) every 5 days every month, starting on the first day of the month |
0 11 11 11 11 ? |
Fire every November 11th at 11:11am |
Note:Be mindful of setting times between the hours of the morning when the change to or from “daylight saving time” begins (for U.S. locales, this is between 1 am and 3 am) because the time shift can cause a skip or a repeat depending on whether the time moves back or jumps forward.
See Also