Quartz cron format
A Quartz expression has six or seven fields: seconds, minutes, hours, day of month, month, day of week and an optional year. For example 0 0 12 ? * MON-FRI fires at noon on weekdays. Days of the week are numbered 1 (Sunday) to 7 (Saturday), and names such as MON or FRI are accepted, including in ranges.
The question mark rule
Quartz does not let you constrain both the day of month and the day of week. Exactly one of these two fields must contain “?”, which means “no specific value”. Writing 0 0 12 * * MON is therefore invalid; use 0 0 12 ? * MON instead. The explainer flags this immediately.
Special characters L, W and #
L means “last”: L in the day-of-month field is the last day of the month, L-3 is three days before it, and 6L in the day-of-week field is the last Friday. W finds the nearest weekday to a date without leaving the month, so 15W fires on the weekday closest to the 15th, and LW is the last weekday of the month. The hash selects the nth weekday: 6#3 is the third Friday.
Quartz in practice
In Java code a Quartz expression is attached to a trigger, for example TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0 0 12 ? * MON-FRI").inTimeZone(TimeZone.getTimeZone("Europe/London"))). Without inTimeZone the JVM default zone is used. Misfire instructions decide what happens when the scheduler was down at a planned time: fire once now, or skip to the next slot.
Frequently asked questions
- How is Quartz cron different from Unix cron?
- Quartz adds a seconds field at the start and an optional year at the end, numbers weekdays from 1 = Sunday, requires “?” in one day field and supports L, W and #.
- Can I convert a Quartz expression to crontab?
- Yes, as long as the seconds are 0, no year is set and L, W or # aren’t used. The converter shows the crontab equivalent or explains why there isn’t one.
- What does 0 0/5 * * * ? mean?
- It fires every five minutes, at second 0, starting at minute 0 of every hour, every day.
- Is Spring’s @Scheduled cron the same as Quartz?
- Very close, but Spring numbers Sunday as 0 or 7, has no year field and doesn’t require “?”. Use the Spring page for Spring expressions.