Cron Expression
A cron expression is a string that represents the schedule for the execution of a job. A cron expression consists of six or seven fields (time units).
1. Cron Expression Structure
A Cron Expression consists of seven fields separated by spaces:
<second> <minute> <hour> <day-of-month> <month> <day-of-week> <year>
Field | Required? | Allowed Values | Special Characters | Description |
---|---|---|---|---|
Seconds | Required | 0-59 |
, - * / |
Specifies the second. |
Minutes | Required | 0-59 |
, - * / |
Specifies the minute. |
Hours | Required | 0-23 |
, - * / |
Specifies the hour. |
Day-of-Month | Required | 1-31 |
, - * ? / L W |
Specifies the day of the month. |
Month | Required | 1-12 or JAN-DEC |
, - * / |
Specifies the month. |
Day-of-Week | Required | 1-7 or SUN-SAT |
, - * ? / L # |
Specifies the day of the week. |
Year | Optional | 1970-2099 |
, - * / |
Specifies the year. Automatically generate, the tool does not display this value. |
2. Special Characters and Their Meanings
Cron Expressions support several special characters to define complex scheduling rules:
*
(Asterisk)
- Matches all possible values for the field.
- Example:
* * * * * ?
(Every second of every minute of every hour)
,
(Comma)
- Specifies a list of values.
- Example:
0 0 9,15 * * ?
(At 9:00 AM and 3:00 PM every day)
-
(Dash)
- Specifies a range of values.
- Example:
0 0 10-12 * * ?
(Every hour between 10:00 AM and 12:00 PM)
/
(Slash)
- Specifies increments.
- Example:
0 0/15 * * * ?
(Every 15 minutes)
?
(Question Mark)
- Used in either the Day-of-Month or Day-of-Week fields to specify "no specific value."
- Example:
0 0 10 ? * MON-FRI
(At 10:00 AM Monday through Friday)
L
(Last)
- Used in Day-of-Month or Day-of-Week fields to specify the last day.
- Example:
0 0 0 L * ?
(Midnight on the last day of every month) - Example:
0 0 0 ? * 5L
(Midnight on the last Friday of every month)
W
(Weekday)
- Specifies the nearest weekday (Monday to Friday) to a given day.
- Example:
0 0 0 15W * ?
(Midnight on the nearest weekday to the 15th of the month)
#
(Nth Day of Week)
- Specifies the nth occurrence of a day in a month.
- Example:
0 0 0 ? * 2#1
(Midnight on the first Monday of every month)
.3 Practical Examples
Here are some practical examples Cron Expressions:
Every second:
* * * * * ?
Every minute:
0 * * * * ?
Every day at midnight:
0 0 0 * * ?
Every 5 minutes between 9:00 AM and 5:00 PM:
0 0/5 9-17 * * ?
At 10:30 AM on the 1st and 15th of every month:
0 30 10 1,15 * ?
At 8:00 AM on the last Friday of every month:
0 0 8 ? * 6L
At noon on weekdays:
0 0 12 ? * MON-FRI
Every 30 seconds starting at 5 seconds past the minute:
5/30 * * * * ?
4. Common Mistakes and Best Practices
1. Day-of-Month and Day-of-Week Conflict
- Avoid specifying both fields simultaneously.
- Use
?
in one of the fields to avoid ambiguity.
2. February Dates
- Ensure the Day-of-Month value is valid for February (e.g.,
29
only works in leap years).
3. Year Field
- The Year field is optional. If omitted, the Cron Expression will run for all years.
4. Overflowing ranges is supported
- that is, having a larger number on the left hand side than the right. You might do 22-2 to catch 10 o'clock at night until 2 o'clock in the morning, or you might have NOV-FEB.
- It is very important to note that overuse of overflowing ranges creates ranges that don't make sense and no effort has been made to determine which interpretation CronExpression chooses.