Spring error A range that runs backwards
Minimum value must be less than maximum value '22-2' in cron expression "0 0 22-2 * * *"
java.lang.IllegalStateException: Encountered invalid @Scheduled method 'run': Minimum value must be less than maximum value '22-2' in cron expression "0 0 22-2 * * *"java.lang.IllegalStateException: Encountered invalid @Scheduled method 'run': Minimum value must be less than maximum value '5-1' in cron expression "0 0 9 * * FRI-MON"Message produced by running the expression through org.springframework.scheduling.support.CronExpression from Spring Framework 7.0.10, the parser behind @Scheduled. 6.2.19 prints the same.
What it means
Every range in a Spring cron field has to run from low to high. A range that wraps past the end of the field, such as 22-2 for “10 PM to 2 AM” or FRI-MON for a long weekend, is rejected. Quartz and EventBridge accept wrapping ranges, so expressions copied from them often hit this.
The quoted range in the message is the one Spring read, after swapping names for numbers: FRI-MON appears as 5-1, because Spring counts Monday as 1 and Sunday as 7.
Rejected expressions and their fixes
Each expression opens in the tester. The message under a rejected one is what Spring Framework 7.0.10 printed for it.
-
Rejected0 0 22-2 * * *Minimum value must be less than maximum value '22-2' in cron expression "0 0 22-2 * * *"Use0 0 22-23,0-2 * * *At 00:00, 01:00, 02:00, 22:00 and 23:00.
Hourly from 22:00 to 02:00. Split the range at midnight into a list. Each day’s early hours belong to that day, not to the night before.
-
Rejected0 0 9 * * FRI-MONMinimum value must be less than maximum value '5-1' in cron expression "0 0 9 * * FRI-MON"Use0 0 9 * * FRI-SUN,MONAt 09:00, on Sundays and Mondays and Fridays and Saturdays.
Friday to Monday.
FRI-SUNis fine because Sunday is 7, the end of the week. -
Rejected0 0 0 1 NOV-FEB *Minimum value must be less than maximum value '11-2' in cron expression "0 0 0 1 NOV-FEB *"Use0 0 0 1 NOV-DEC,JAN-FEB *At 00:00, on the 1st of the month, in January and February and November and December.
November to February. Split at the end of the year.
Questions
- Does
7-2count as a backwards range? - No. A range that starts at 7 in day of week starts at Sunday 0 instead, so
7-2means Sunday to Tuesday and is accepted. - Why does Quartz accept the same range?
- Quartz deliberately supports ranges that overflow the field, such as
22-2andNOV-FEB. Spring’s parser builds ajava.timevalue range, which must run from low to high.
Other Spring errors
- Cron expression must consist of 6 fields (found 5 in "*/5 * * * *")Spring: Five or seven fields
- Invalid value for HourOfDay (valid values 0 - 23): 24 '24' in cron expression "0 0 24 * * *"Spring: A value outside the field
- For input string: "?" '?' in cron expression "0 ? * * * *"Spring: Something that is not a number
- No day-of-week before 'L' in 'L' in cron expression "0 0 0 * * L"Spring:
Lalone in day of week