修复CronPatternUtil.nextDateAfter计算下一个匹配表达式的日期时,计算错误问题(issue#4006@Github)

This commit is contained in:
Looly
2025-08-21 10:08:11 +08:00
parent f0eb62e4e5
commit e31658ce9e
3 changed files with 7 additions and 13 deletions

View File

@@ -2,7 +2,7 @@
# 🚀Changelog
-------------------------------------------------------------------------------------------------------------
# 5.8.40(2025-08-20)
# 5.8.40(2025-08-21)
### 🐣新特性
* 【captcha】 `MathGenerator`四则运算方式支持不生成负数结果pr#1363@Gitee
@@ -20,6 +20,7 @@
* 【core 】 修复`TreeBuilder`append重复向idTreeMap中put问题pr#3992@Github
* 【extra 】 修复`QLExpressEngine`allowClassSet无效问题issue#3994@Github
* 【core 】 修复`StrBuilder`insert插入计算错误问题issue#ICTSRZ@Gitee
* 【cron 】 修复`CronPatternUtil.nextDateAfter`计算下一个匹配表达式的日期时计算错误问题issue#4006@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.39(2025-06-20)

View File

@@ -192,7 +192,7 @@ public class PatternMatcher {
// pr#1189
if (i == Part.DAY_OF_MONTH.ordinal()
&& matchers[i] instanceof DayOfMonthMatcher
&& ((DayOfMonthMatcher) matchers[i]).isLastDay(values[i],values[i+1],isLeapYear(values[Part.YEAR.ordinal()]))) {
&& ((DayOfMonthMatcher) matchers[i]).isLastDay(values[i],values[i+1],DateUtil.isLeapYear(values[Part.YEAR.ordinal()]))) {
int newMonth = newValues[Part.MONTH.ordinal()];
int newYear = newValues[Part.YEAR.ordinal()];
nextValue = getLastDay(newMonth, newYear);
@@ -226,7 +226,7 @@ public class PatternMatcher {
continue;
} else if (i == Part.DAY_OF_MONTH.ordinal()
&& matchers[i] instanceof DayOfMonthMatcher
&& ((DayOfMonthMatcher) matchers[i]).isLastDay(values[i],values[i+1],isLeapYear(values[Part.YEAR.ordinal()]))) {
&& ((DayOfMonthMatcher) matchers[i]).isLastDay(values[i],values[i+1],DateUtil.isLeapYear(values[Part.YEAR.ordinal()]))) {
int newMonth = newValues[Part.MONTH.ordinal()];
int newYear = newValues[Part.YEAR.ordinal()];
nextValue = getLastDay(newMonth, newYear);
@@ -247,14 +247,6 @@ public class PatternMatcher {
return newValues;
}
/**
* 判断年份是否是闰年
* @param year
* @return 返回boolean表示是否是闰年
*/
private static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
/**
* 设置从{@link Part#SECOND}到指定部分,全部设置为最小值
*

View File

@@ -1,6 +1,7 @@
package cn.hutool.cron.pattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Console;
import org.junit.jupiter.api.Test;
import java.util.Date;
@@ -13,8 +14,8 @@ public class Issue4006Test {
DateTime judgeTime = DateTime.of(new Date());
CronPattern cronPattern = new CronPattern(cron);
System.out.println("cronPattern = " + cronPattern);
Console.log("cronPattern = " + cronPattern);
Date nextDate = CronPatternUtil.nextDateAfter(cronPattern, judgeTime, true);
System.out.println("nextDate = " + nextDate);
Console.log("nextDate = " + nextDate);
}
}