BackendBench
Coffee
Spot the Bug #46

Monday to Friday that is Sunday to Thursday

A hard 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.

hardScheduling and cron mistakesbuggy line: 8

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

What is wrong

In Quartz, day-of-week numbers run from 1 (Sunday) to 7 (Saturday). So 1-5 means Sunday to Thursday: the job runs on Sunday and skips Friday. In Unix cron the same range would mean Monday to Friday.

The fix

String expression = "0 0 9 ? * MON-FRI";

How to avoid it next time

Use day names. Number ranges differ between Quartz, Unix and Spring, and the bug is easy to miss in review.

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โ† #45#47 โ†’

Frequently asked questions

What is the bug in "Monday to Friday that is Sunday to Thursday"?

In Quartz, day-of-week numbers run from 1 (Sunday) to 7 (Saturday). So 1-5 means Sunday to Thursday: the job runs on Sunday and skips Friday. In Unix cron the same range would mean Monday to Friday. The faulty line is line 8: String expression = "0 0 9 ? * 1-5";

How do I fix it?

Replace it with: String expression = "0 0 9 ? * MON-FRI";. Use day names. Number ranges differ between Quartz, Unix and Spring, and the bug is easy to miss in review.

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