diff --git a/spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java b/spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java index 59043762dc46..9af70129bd05 100644 --- a/spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java +++ b/spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java @@ -177,7 +177,11 @@ private static ValueRange parseRange(String value, Type type) { int next = nextSetBit(current); if (next == -1) { temporal = type().rollForward(temporal); - next = nextSetBit(0); + next = nextSetBit(type().get(temporal)); + if (next == -1) { + temporal = type().rollForward(temporal); + next = nextSetBit(0); + } } if (next == current) { return temporal; @@ -191,7 +195,12 @@ private static ValueRange parseRange(String value, Type type) { next = nextSetBit(current); if (next == -1) { temporal = type().rollForward(temporal); - next = nextSetBit(0); + next = nextSetBit(type().get(temporal)); + if (next == -1) { + temporal = type().rollForward(temporal); + next = nextSetBit(0); + } + current = type().get(temporal); } } if (count >= CronExpression.MAX_ATTEMPTS) { diff --git a/spring-context/src/test/java/org/springframework/scheduling/support/BitsCronFieldTests.java b/spring-context/src/test/java/org/springframework/scheduling/support/BitsCronFieldTests.java index d410d44de6e9..7361054fc3ec 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/support/BitsCronFieldTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/support/BitsCronFieldTests.java @@ -16,6 +16,7 @@ package org.springframework.scheduling.support; +import java.time.ZonedDateTime; import java.util.Arrays; import org.assertj.core.api.Condition; @@ -29,6 +30,7 @@ * * @author Arjen Poutsma * @author Sam Brannen + * @author Brian Clozel */ class BitsCronFieldTests { @@ -112,6 +114,24 @@ void names() { .has(clear(0)).has(setRange(1, 7)); } + @Test + void nextOrSameWithMidnightGap() { + BitsCronField field = BitsCronField.parseHours("0-23/2"); + ZonedDateTime last = ZonedDateTime.parse("2025-04-24T23:00:00+02:00[Africa/Cairo]"); + ZonedDateTime expected = ZonedDateTime.parse("2025-04-25T02:00:00+03:00[Africa/Cairo]"); + ZonedDateTime actual = field.nextOrSame(last); + assertThat(actual).isEqualTo(expected); + } + + @Test + void nextOrSameWithGapAfterRollForward() { + BitsCronField field = BitsCronField.parseHours("0,2"); + ZonedDateTime last = ZonedDateTime.parse("2026-03-08T01:00:00-05:00[America/New_York]"); + ZonedDateTime expected = ZonedDateTime.parse("2026-03-09T00:00:00-04:00[America/New_York]"); + ZonedDateTime actual = field.nextOrSame(last); + assertThat(actual).isEqualTo(expected); + } + private static Condition set(int... indices) { return new Condition<>(String.format("set bits %s", Arrays.toString(indices))) { diff --git a/spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java b/spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java index a60c64e2526f..c35abbee6541 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/support/CronExpressionTests.java @@ -1367,6 +1367,14 @@ void daylightSaving() { actual = cronExpression.next(last); assertThat(actual).isNotNull(); assertThat(actual).isEqualTo(expected); + + cronExpression = CronExpression.parse("0 0 */2 * * ?"); + + last = ZonedDateTime.parse("2025-04-24T22:00:00+02:00[Africa/Cairo]"); + expected = ZonedDateTime.parse("2025-04-25T02:00:00+03:00[Africa/Cairo]"); + actual = cronExpression.next(last); + assertThat(actual).isNotNull(); + assertThat(actual).isEqualTo(expected); } @Test