This commit is contained in:
Looly 2022-03-26 01:21:58 +08:00
parent 75f4ca356d
commit 48a99942d7
2 changed files with 48 additions and 1 deletions

View File

@ -79,7 +79,6 @@ public class CronPattern {
}
// --------------------------------------------------------------------------------------- match start
/**
* 给定时间是否匹配定时任务表达式
*

View File

@ -1,5 +1,7 @@
package cn.hutool.cron.pattern.matcher;
import cn.hutool.core.collection.CollUtil;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ -27,6 +29,52 @@ public class MatcherTable {
}
public LocalDateTime nextMatchAfter(int second, int minute, int hour, int dayOfMonth, int month, int dayOfWeek, int year) {
List<LocalDateTime> nextMatchs = new ArrayList<>(second);
for (DateTimeMatcher matcher : matchers) {
nextMatchs.add(singleNextMatchAfter(matcher, second, minute, hour,
dayOfMonth, month, dayOfWeek, year));
}
// 返回最先匹配到的日期
return CollUtil.min(nextMatchs);
}
private static LocalDateTime singleNextMatchAfter(DateTimeMatcher matcher, int second, int minute, int hour,
int dayOfMonth, int month, int dayOfWeek, int year){
boolean isNextNotEquals = true;
//
final int nextYear = matcher.yearMatcher.nextAfter(year);
isNextNotEquals &= (year != nextYear);
//
int nextDayOfWeek;
if(isNextNotEquals){
// 上一个字段不一致说明产生了新值本字段使用最小值
nextDayOfWeek = ((BoolArrayValueMatcher)matcher.dayOfWeekMatcher).getMinValue();
}else{
nextDayOfWeek = matcher.dayOfWeekMatcher.nextAfter(dayOfWeek);
isNextNotEquals &= (dayOfWeek != nextDayOfWeek);
}
//
int nextMonth;
if(isNextNotEquals){
// 上一个字段不一致说明产生了新值本字段使用最小值
nextMonth = ((BoolArrayValueMatcher)matcher.monthMatcher).getMinValue();
}else{
nextMonth = matcher.monthMatcher.nextAfter(dayOfWeek);
isNextNotEquals &= (month != nextMonth);
}
//
int nextDayOfMonth;
if(isNextNotEquals){
// 上一个字段不一致说明产生了新值本字段使用最小值
nextDayOfMonth = ((BoolArrayValueMatcher)matcher.dayOfMonthMatcher).getMinValue();
}else{
nextDayOfMonth = matcher.dayOfMonthMatcher.nextAfter(dayOfWeek);
isNextNotEquals &= (dayOfMonth != nextDayOfMonth);
}
return null;
}