1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00

修复CronPatternUtil.nextDateAfter栈溢出问题

This commit is contained in:
Looly 2024-08-08 19:15:24 +08:00
parent 57d9a415fc
commit 4c00f6adb2
4 changed files with 60 additions and 5 deletions
CHANGELOG.md
hutool-cron/src
main/java/cn/hutool/cron/pattern
test/java/cn/hutool/cron/pattern

View File

@ -31,6 +31,7 @@
* 【core 】 修复CsvParser中对正文中双引号处理逻辑问题pr#1244@Gitee
* 【core 】 修复ZipUtil.zip压缩到本目录时可能造成的死循环问题issue#IAGYDG@Gitee
* 【cache 】 修复AbstractCache.get中锁不一致导致的并发问题issue#3686@Github
* 【cron 】 修复CronPatternUtil.nextDateAfter栈溢出问题issue#3685@Github
-------------------------------------------------------------------------------------------------------------
# 5.8.29(2024-07-03)

View File

@ -156,13 +156,25 @@ public class CronPattern {
calendar = newCalendar;
}
return nextMatch(calendar);
}
/**
* 返回匹配到的下一个时间如果给定时间匹配直接返回
*
* @param calendar 时间
* @return 匹配到的下一个时间
* @since 5.8.30
*/
public Calendar nextMatch(final Calendar calendar) {
Calendar next = nextMatchAfter(PatternUtil.getFields(calendar, true), calendar.getTimeZone());
if (false == match(next, true)) {
next.set(Calendar.DAY_OF_MONTH, next.get(Calendar.DAY_OF_MONTH) + 1);
next = CalendarUtil.beginOfDay(next);
return nextMatchAfter(next);
if (match(next, true)) {
return next;
}
return next;
next.set(Calendar.DAY_OF_MONTH, next.get(Calendar.DAY_OF_MONTH) + 1);
next = CalendarUtil.beginOfDay(next);
return nextMatch(next);
}
@Override

View File

@ -17,6 +17,18 @@ import java.util.List;
*/
public class CronPatternUtil {
/**
* 列举指定日期之后内第一个匹配表达式的日期
*
* @param pattern 表达式
* @param start 起始时间
* @return 日期
* @since 5.8.30
*/
public static Date nextDateAfter(CronPattern pattern, Date start) {
return DateUtil.date(pattern.nextMatchAfter(CalendarUtil.calendar(start)));
}
/**
* 列举指定日期之后内第一个匹配表达式的日期
*
@ -25,7 +37,9 @@ public class CronPatternUtil {
* @param isMatchSecond 是否匹配秒无效
* @return 日期
* @since 4.5.8
* @deprecated isMatchSecond无效使用 {@link #nextDateAfter(CronPattern, Date)}
*/
@Deprecated
public static Date nextDateAfter(CronPattern pattern, Date start, boolean isMatchSecond) {
return DateUtil.date(pattern.nextMatchAfter(CalendarUtil.calendar(start)));
}

View File

@ -0,0 +1,28 @@
package cn.hutool.cron.pattern;
import cn.hutool.core.date.DateUtil;
import org.junit.Test;
import java.util.Date;
import static org.junit.Assert.assertEquals;
public class Issue3685Test {
@Test
public void nextDateAfterTest() {
Date date = CronPatternUtil.nextDateAfter(CronPattern.of("0 0 * * MON"), DateUtil.parse("2024-08-01"));
assertEquals("2024-08-05 00:00:00", date.toString());
date = CronPatternUtil.nextDateAfter(CronPattern.of("0 0 * * MON"), DateUtil.parse("2024-08-02"));
assertEquals("2024-08-05 00:00:00", date.toString());
date = CronPatternUtil.nextDateAfter(CronPattern.of("0 0 * * MON"), DateUtil.parse("2024-08-03"));
assertEquals("2024-08-05 00:00:00", date.toString());
date = CronPatternUtil.nextDateAfter(CronPattern.of("0 0 * * MON"), DateUtil.parse("2024-08-04"));
assertEquals("2024-08-05 00:00:00", date.toString());
date = CronPatternUtil.nextDateAfter(CronPattern.of("0 0 * * MON"), DateUtil.parse("2024-08-05"));
assertEquals("2024-08-12 00:00:00", date.toString());
}
}