Quartz-style Nth Day of Week cron expressions can overflow to other month #34360
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: backported
An issue that has been backported to maintenance branches
type: bug
A general bug
Milestone
The issue
We are experiencing a weird issue with Cron Expressions. We are using the
@Scheduled
annotation to run scheduled jobs every monday.We have a use case where we want to run a schedule differently if it is the first Monday of the month, which is why we split our schedules up into 2 CRON expressions, using the "day of week" notation (documented here)
0 0 8 ? * MON#1
- run every first Monday of the month0 0 8 ? * MON#2,MON#3,MON#4,MON#5
- run on all other Mondays of the monthThis worked fine until we observed weird behavior today on Monday the 3rd February 2025. The second CRON expression fired even though it should not fire on the first Monday of a month.
I run a test for the next 10000 invocations and these are the 10 dates that this unexpected overlap will happen next:
Conclusion
It seems as if the day of week expression (e.g.
MON#5
) overflows only from January to February in the years where January has less than 5 Mondays. (This is why 31.1.2028 is not up there for example).Reproduction
This can be reproduced with spring-context-6.2.2 with following test case:
The text was updated successfully, but these errors were encountered: