Skip to content

Commit 523785f

Browse files
authored
Merge pull request #398 from pangyikhei/394-spring-cron
When generating day candidates, take the intersection of days generated from DoW and DoM instead of union
2 parents bf8ad84 + 793260a commit 523785f

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/main/java/com/cronutils/model/time/SingleExecutionTime.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,11 @@ private List<Integer> generateDayCandidatesQuestionMarkSupportedUsingDoWAndDoM(f
691691
.stream().distinct().sorted()
692692
.collect(Collectors.toList());
693693
} else {
694-
return Stream.concat(
695-
createDayOfWeekValueGeneratorInstance(daysOfWeekCronField, year, month, mondayDoWValue).generateCandidates(1, lengthOfMonth).stream(),
696-
createDayOfMonthValueGeneratorInstance(daysOfMonthCronField, year, month).generateCandidates(1, lengthOfMonth).stream()
697-
).distinct().sorted().collect(Collectors.toList());
694+
Set<Integer> candidates = new HashSet<>(createDayOfMonthValueGeneratorInstance(daysOfMonthCronField, year, month).generateCandidates(1, lengthOfMonth));
695+
Set<Integer> daysOfWeek = new HashSet<>(createDayOfWeekValueGeneratorInstance(daysOfWeekCronField, year, month, mondayDoWValue).generateCandidates(1, lengthOfMonth));
696+
// Only the intersection of valid days from the days of week and valid days of month should be returned.
697+
candidates.retainAll(daysOfWeek);
698+
return candidates.stream().sorted().collect(Collectors.toList());
698699
}
699700
}
700701

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.cronutils;
2+
3+
import com.cronutils.model.definition.CronDefinition;
4+
import com.cronutils.model.definition.CronDefinitionBuilder;
5+
import com.cronutils.model.time.ExecutionTime;
6+
import com.cronutils.parser.CronParser;
7+
import org.junit.Test;
8+
9+
import java.time.DayOfWeek;
10+
import java.time.ZonedDateTime;
11+
import java.util.Optional;
12+
13+
import static com.cronutils.model.CronType.SPRING;
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertTrue;
16+
17+
public class Issue394Test {
18+
19+
@Test
20+
public void testEveryMondayAt0900hours() {
21+
String cron = "0 0 9 * * MON";
22+
23+
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(SPRING);
24+
CronParser parser = new CronParser(cronDefinition);
25+
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(cron));
26+
ZonedDateTime now = ZonedDateTime.now();
27+
Optional<ZonedDateTime> nextExecution = executionTime.nextExecution(now);
28+
assertTrue(nextExecution.isPresent());
29+
// Should be the next Monday at 0900 hours
30+
assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek());
31+
assertEquals(9, nextExecution.get().getHour());
32+
// The next execution after that should also be a Monday at 0900 hours, the following week
33+
ZonedDateTime nextExpectedExecution = nextExecution.get().plusWeeks(1);
34+
nextExecution = executionTime.nextExecution(nextExecution.get());
35+
assertTrue(nextExecution.isPresent());
36+
assertEquals(DayOfWeek.MONDAY, nextExecution.get().getDayOfWeek());
37+
assertEquals(9, nextExecution.get().getHour());
38+
assertEquals(nextExpectedExecution, nextExecution.get());
39+
}
40+
}

0 commit comments

Comments
 (0)