Skip to content

fix: skip short months for nW day-of-month (#715) - #720

Open
arimu1 wants to merge 1 commit into
jmrozanec:masterfrom
arimu1:fix/715-nw-short-months
Open

fix: skip short months for nW day-of-month (#715)#720
arimu1 wants to merge 1 commit into
jmrozanec:masterfrom
arimu1:fix/715-nw-short-months

Conversation

@arimu1

@arimu1 arimu1 commented Aug 8, 2026

Copy link
Copy Markdown

Summary

Fixes #715

nW (nearest weekday to the nth of the month) threw DateTimeException when the candidate month was shorter than n (e.g. 31W in June), because OnDayOfMonthValueGenerator called LocalDate.of(year, month, dayOfMonth) before checking month length.

Changes

  • Guard: if dayOfMonth > lengthOfMonth, throw NoSuchValueException so the scheduler skips that month (same approach as the L branch, and aligned with Quartz).
  • Sunday last day: when n is the last day of a long month and falls on Sunday, resolve to Friday (n - 2) so weekday adjustment matches Quartz (e.g. 2025-08-312025-08-29).

Reproduction (now fixed)

CronParser parser = new CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ));
ExecutionTime et = ExecutionTime.forCron(parser.parse("0 0 0 31W * ?"));
et.nextExecution(ZonedDateTime.parse("2025-06-01T00:00:00Z"));
// → 2025-07-31 (June/Sep/Nov/Feb skipped)

Test plan

  • OnDayOfMonthValueGeneratorWTest — short-month no-value + last-day Sunday → Friday
  • Issue715Test — Quartz 31W sequence matches reference; SPRING53 skips June
  • Full suite: 809 tests, 0 failures (Temurin 21)

Guard OnDayOfMonthValueGenerator W when day exceeds month length so
LocalDate.of no longer throws DateTimeException (e.g. 31W in June).
Also use Friday when n falls on the last day and is Sunday, matching
Quartz nearest-weekday behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nW in day of month throws DateTimeException when the month is shorter than n

1 participant