This commit is contained in:
Looly
2022-04-08 08:12:33 +08:00
parent b419d8e5f5
commit 72a8e3740c
5 changed files with 38 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
package cn.hutool.cron.pattern;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -8,6 +10,22 @@ import java.util.Calendar;
public class CronPatternNextMatchTest {
@Test
public void nextMatchAllAfterTest() {
// 匹配所有,返回下一秒的时间
CronPattern pattern = new CronPattern("* * * * * * *");
DateTime date = DateUtil.truncate(DateUtil.date(), DateField.SECOND);
Calendar calendar = pattern.nextMatchAfter(date.toCalendar());
Assert.assertEquals(date.getTime(), DateUtil.date(calendar).getTime());
// 匹配所有分,返回下一分钟
pattern = new CronPattern("0 * * * * * *");
date = DateUtil.parse("2022-04-08 07:44:16");
//noinspection ConstantConditions
calendar = pattern.nextMatchAfter(date.toCalendar());
Assert.assertEquals(DateUtil.parse("2022-04-08 07:45:00"), DateUtil.date(calendar));
}
@Test
public void nextMatchAfterTest(){
CronPattern pattern = new CronPattern("23 12 * 12 * * *");
@@ -20,14 +38,21 @@ public class CronPatternNextMatchTest {
Assert.assertTrue(pattern.match(calendar, true));
Assert.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
// 秒超出规定值的最大值,小时+1
// 秒超出规定值的最大值,分+1秒取最小值
//noinspection ConstantConditions
calendar = pattern.nextMatchAfter(
DateUtil.parse("2022-04-12 09:09:24").toCalendar());
Assert.assertTrue(pattern.match(calendar, true));
Assert.assertEquals("2022-04-12 09:12:23", DateUtil.date(calendar).toString());
// 秒超出规定值的最大值,分不变,小时+1秒和分使用最小值
//noinspection ConstantConditions
calendar = pattern.nextMatchAfter(
DateUtil.parse("2022-04-12 09:12:24").toCalendar());
Assert.assertTrue(pattern.match(calendar, true));
Assert.assertEquals("2022-04-12 10:12:23", DateUtil.date(calendar).toString());
// 天超出规定值的最大值,月+1
// 天超出规定值的最大值,月+1,天、时、分、秒取最小值
//noinspection ConstantConditions
calendar = pattern.nextMatchAfter(
DateUtil.parse("2022-04-13 09:12:24").toCalendar());