Quartz Expressions: Complete Guide
“Quartz expressions are time expressions used for scheduling tasks, allowing you to execute tasks at specific time points. Although the syntax of Quartz expressions is complex, they provide highly flexible and precise time control.”
Quartz expressions consist of six fields representing seconds, minutes, hours, date, month, and day of the week, each with different value ranges that can be specified using wildcards (*), ranges (-), increments (/), and lists (,).
Here is the detailed syntax of the Quartz expression:
- Seconds (0-59): Specifies the number of seconds at which the task should be executed. For example, “0/5” means the task will be executed every 5 seconds.
- Minutes (0-59): specifies the number of minutes for a scheduled task to execute. For example, “*/10” means the task will run every 10 minutes.
- Hour (0-23): the specified hour at which a task is to be executed. For example, “9-17” means to execute between 9 in the morning and 5 in the afternoon.
- Date (1-31): Specifies the date on which the task will be executed. For example, “1,15” means it will be executed on the 1st and 15th of every month.
- Month (1-12): specifies the month for task execution. For example, “1-6” indicates execution between January and June.
- Week (1-7, where 1 represents Sunday, 2 represents Monday): specifies the day of the week when the task will be executed. For example, “1-5” means it will be executed from Monday to Friday.
In addition to basic field values, Quartz expressions also support some special characters and symbols.
- The question mark can be used as a wildcard to represent any value in date and day fields.
- *: represents all possible values.
- /: represents increment, used to specify the increment interval of a range of values, for example, “0/5” indicates starting from 0, and incrementing every 5 seconds.
- Colon is used to indicate a range, specifying an interval value, such as “9-17” indicating from 9 to 17.
- The comma is used to list multiple values, for example, “1,15” represents 1 and 15.
- L: It represents the end for date and week fields, for example, “L” represents the last day or last week.
- W: Represents weekdays, used in the date field, for example “15W” represents the closest weekday to the 15th.
- # : Indicates which week of the month, used for the weekday field, for example, “2#2” indicates the second Tuesday of the month.
By combining and using these symbols, very precise and flexible Quartz expressions can be generated to meet the needs of various scheduling tasks.