mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复CronPatternUtil.nextDateAfter栈溢出问题
This commit is contained in:
parent
67463d5dc4
commit
9eb95bf9d7
@ -164,13 +164,24 @@ public class CronPattern {
|
||||
calendar = newCalendar;
|
||||
}
|
||||
|
||||
return nextMatch(calendar);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回匹配到的下一个时间,如果给定时间匹配,直接返回
|
||||
*
|
||||
* @param calendar 时间
|
||||
* @return 匹配到的下一个时间
|
||||
*/
|
||||
public Calendar nextMatch(final Calendar calendar) {
|
||||
Calendar next = nextMatchAfter(PatternUtil.getFields(calendar, true), calendar.getTimeZone());
|
||||
if (!match(next, true)) {
|
||||
if (match(next, true)) {
|
||||
return next;
|
||||
}
|
||||
|
||||
next.set(Calendar.DAY_OF_MONTH, next.get(Calendar.DAY_OF_MONTH) + 1);
|
||||
next = CalendarUtil.beginOfDay(next);
|
||||
return nextMatchAfter(next);
|
||||
}
|
||||
return next;
|
||||
return nextMatch(next);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,6 +16,7 @@ import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.cron.pattern.Part;
|
||||
|
||||
import java.time.Year;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
@ -170,6 +171,13 @@ public class PatternMatcher {
|
||||
return calendar;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PatternMatcher{" +
|
||||
"matchers=" + Arrays.toString(matchers) +
|
||||
'}';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一个匹配日期时间<br>
|
||||
* 获取方法是,先从年开始查找对应部分的下一个值:
|
||||
|
@ -0,0 +1,28 @@
|
||||
package org.dromara.hutool.cron.pattern;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class Issue3685Test {
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user