mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复CronPattern.nextMatchAfter匹配初始值问题
This commit is contained in:
parent
c02460423e
commit
0942cf2c60
@ -2,7 +2,7 @@
|
|||||||
# 🚀Changelog
|
# 🚀Changelog
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.28(2024-04-21)
|
# 5.8.28(2024-04-22)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
* 【core 】 修正XmlUtil的omitXmlDeclaration描述注释(issue#I9CPC7@Gitee)
|
||||||
@ -22,6 +22,7 @@
|
|||||||
* 【core 】 修复RandomUtil.randomStringWithoutStr方法问题(pr#1209@Gitee)
|
* 【core 】 修复RandomUtil.randomStringWithoutStr方法问题(pr#1209@Gitee)
|
||||||
* 【http 】 修复HttpRequest.header相同key被覆盖问题(issue#I9I61C@Gitee)
|
* 【http 】 修复HttpRequest.header相同key被覆盖问题(issue#I9I61C@Gitee)
|
||||||
* 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee)
|
* 【core 】 修复TemporalAccessorConverter自定义格式转换问题(issue#I9HQQE@Gitee)
|
||||||
|
* 【cron 】 修复CronPattern.nextMatchAfter匹配初始值问题(issue#I9FQUA@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
# 5.8.27(2024-03-29)
|
# 5.8.27(2024-03-29)
|
||||||
|
@ -149,6 +149,13 @@ public class CronPattern {
|
|||||||
* @return 匹配到的下一个时间
|
* @return 匹配到的下一个时间
|
||||||
*/
|
*/
|
||||||
public Calendar nextMatchAfter(Calendar calendar) {
|
public Calendar nextMatchAfter(Calendar calendar) {
|
||||||
|
// issue#I9FQUA,当提供的时间已经匹配表达式时,增加1秒以匹配下一个时间
|
||||||
|
if(match(calendar, true)){
|
||||||
|
final Calendar newCalendar = Calendar.getInstance(calendar.getTimeZone());
|
||||||
|
newCalendar.setTimeInMillis(calendar.getTimeInMillis() + 1000);
|
||||||
|
calendar = newCalendar;
|
||||||
|
}
|
||||||
|
|
||||||
Calendar next = nextMatchAfter(PatternUtil.getFields(calendar, true), calendar.getTimeZone());
|
Calendar next = nextMatchAfter(PatternUtil.getFields(calendar, true), calendar.getTimeZone());
|
||||||
if (false == match(next, true)) {
|
if (false == match(next, true)) {
|
||||||
next.set(Calendar.DAY_OF_MONTH, next.get(Calendar.DAY_OF_MONTH) + 1);
|
next.set(Calendar.DAY_OF_MONTH, next.get(Calendar.DAY_OF_MONTH) + 1);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package cn.hutool.cron.pattern;
|
package cn.hutool.cron.pattern;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.date.CalendarUtil;
|
||||||
import cn.hutool.core.date.DateUnit;
|
import cn.hutool.core.date.DateUnit;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
@ -18,20 +18,16 @@ import java.util.List;
|
|||||||
public class CronPatternUtil {
|
public class CronPatternUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列举指定日期之后(到开始日期对应年年底)内第一个匹配表达式的日期
|
* 列举指定日期之后内第一个匹配表达式的日期
|
||||||
*
|
*
|
||||||
* @param pattern 表达式
|
* @param pattern 表达式
|
||||||
* @param start 起始时间
|
* @param start 起始时间
|
||||||
* @param isMatchSecond 是否匹配秒
|
* @param isMatchSecond 是否匹配秒(无效)
|
||||||
* @return 日期
|
* @return 日期
|
||||||
* @since 4.5.8
|
* @since 4.5.8
|
||||||
*/
|
*/
|
||||||
public static Date nextDateAfter(CronPattern pattern, Date start, boolean isMatchSecond) {
|
public static Date nextDateAfter(CronPattern pattern, Date start, boolean isMatchSecond) {
|
||||||
List<Date> matchedDates = matchedDates(pattern, start.getTime(), DateUtil.endOfYear(start).getTime(), 1, isMatchSecond);
|
return DateUtil.date(pattern.nextMatchAfter(CalendarUtil.calendar(start)));
|
||||||
if (CollUtil.isNotEmpty(matchedDates)) {
|
|
||||||
return matchedDates.get(0);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,7 +16,7 @@ public class CronPatternNextMatchTest {
|
|||||||
CronPattern pattern = new CronPattern("* * * * * * *");
|
CronPattern pattern = new CronPattern("* * * * * * *");
|
||||||
DateTime date = DateUtil.truncate(DateUtil.date(), DateField.SECOND);
|
DateTime date = DateUtil.truncate(DateUtil.date(), DateField.SECOND);
|
||||||
Calendar calendar = pattern.nextMatchAfter(date.toCalendar());
|
Calendar calendar = pattern.nextMatchAfter(date.toCalendar());
|
||||||
Assert.assertEquals(date.getTime(), DateUtil.date(calendar).getTime());
|
Assert.assertEquals(date.getTime() + 1000, DateUtil.date(calendar).getTime());
|
||||||
|
|
||||||
// 匹配所有分,返回下一分钟
|
// 匹配所有分,返回下一分钟
|
||||||
pattern = new CronPattern("0 * * * * * *");
|
pattern = new CronPattern("0 * * * * * *");
|
||||||
|
@ -0,0 +1,19 @@
|
|||||||
|
package cn.hutool.cron.pattern;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
public class IssueI9FQUATest {
|
||||||
|
@Test
|
||||||
|
public void nextDateAfterTest() {
|
||||||
|
final String cron = "0/5 * * * * ?";
|
||||||
|
final Calendar calendar = CronPattern.of(cron).nextMatchAfter(
|
||||||
|
DateUtil.parse("2024-01-01 00:00:00").toCalendar());
|
||||||
|
|
||||||
|
//Console.log(DateUtil.date(calendar));
|
||||||
|
Assert.assertEquals("2024-01-01 00:00:05", DateUtil.date(calendar).toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user