BackendBench
Coffee
Spot the Bug #44

Quartz wants a question mark

A medium Java puzzle in the scheduling and cron mistakes family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

mediumScheduling and cron mistakesbuggy line: 8

Highlighted: line 8. Play this one as a puzzle without the answer.

What is wrong

Quartz requires one of the day-of-month and day-of-week fields to be ? . Using * in both is rejected with a ParseException at schedule time. The name of the method also says weekdays, which this expression does not express.

The fix

CronScheduleBuilder.cronSchedule("0 0 9 ? * MON-FRI")

How to avoid it next time

In Quartz put ? in the field you do not use, and prefer day names such as MON-FRI to numbers.

Want the background? Read Quartz scheduler in the Java concepts library.

About scheduling and cron mistakes

Cron dialects differ in field count and meaning. Spring has six fields starting with seconds, Unix has five, Quartz needs a question mark in one day field, and time zones decide when midnight actually is.

In code review: Count the fields and check which dialect the framework expects. Confirm the time zone and what the day-of-week field numbers mean in that dialect.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #43#45 โ†’

Frequently asked questions

What is the bug in "Quartz wants a question mark"?

Quartz requires one of the day-of-month and day-of-week fields to be ? . Using * in both is rejected with a ParseException at schedule time. The name of the method also says weekdays, which this expression does not express. The faulty line is line 8: .withSchedule(CronScheduleBuilder.cronSchedule("0 0 9 * * *"))

How do I fix it?

Replace it with: CronScheduleBuilder.cronSchedule("0 0 9 ? * MON-FRI"). In Quartz put ? in the field you do not use, and prefer day names such as MON-FRI to numbers.

How do I catch scheduling and cron mistakes in code review?

Count the fields and check which dialect the framework expects. Confirm the time zone and what the day-of-week field numbers mean in that dialect.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need