修复CronPattern.nextMatchAfter匹配初始值问题

This commit is contained in:
Looly
2024-04-22 01:50:13 +08:00
parent ad2d10223d
commit f3710c7783
8 changed files with 132 additions and 46 deletions

View File

@@ -28,7 +28,7 @@ public class CronPatternNextMatchTest {
CronPattern pattern = new CronPattern("* * * * * * *");
DateTime date = DateUtil.truncate(DateUtil.now(), DateField.SECOND);
Calendar calendar = pattern.nextMatchAfter(date.toCalendar());
Assertions.assertEquals(date.getTime(), DateUtil.date(calendar).getTime());
Assertions.assertEquals(date.getTime() + 1000, DateUtil.date(calendar).getTime());
// 匹配所有分,返回下一分钟
pattern = new CronPattern("0 * * * * * *");

View File

@@ -24,7 +24,7 @@ public class CronPatternUtilTest {
@Test
public void matchedDatesTest() {
//测试每30秒执行
final List<Date> matchedDates = CronPatternUtil.matchedDates("0/30 * 8-18 * * ?", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
final List<Date> matchedDates = CronPatternUtil.matchedDates("0/30 * 8-18 * * ?", DateUtil.parse("2018-10-15 14:33:22"), 5);
Assertions.assertEquals(5, matchedDates.size());
Assertions.assertEquals("2018-10-15 14:33:30", matchedDates.get(0).toString());
Assertions.assertEquals("2018-10-15 14:34:00", matchedDates.get(1).toString());
@@ -36,7 +36,7 @@ public class CronPatternUtilTest {
@Test
public void matchedDatesTest2() {
//测试每小时执行
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 * * *", DateUtil.parse("2018-10-15 14:33:22"), 5, true);
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 * * *", DateUtil.parse("2018-10-15 14:33:22"), 5);
Assertions.assertEquals(5, matchedDates.size());
Assertions.assertEquals("2018-10-15 15:00:00", matchedDates.get(0).toString());
Assertions.assertEquals("2018-10-15 16:00:00", matchedDates.get(1).toString());
@@ -48,7 +48,7 @@ public class CronPatternUtilTest {
@Test
public void matchedDatesTest3() {
//测试最后一天
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 L * *", DateUtil.parse("2018-10-30 23:33:22"), 5, true);
final List<Date> matchedDates = CronPatternUtil.matchedDates("0 0 */1 L * *", DateUtil.parse("2018-10-30 23:33:22"), 5);
Assertions.assertEquals(5, matchedDates.size());
Assertions.assertEquals("2018-10-31 00:00:00", matchedDates.get(0).toString());
Assertions.assertEquals("2018-10-31 01:00:00", matchedDates.get(1).toString());

View File

@@ -27,7 +27,7 @@ public class IssueI82CSHTest {
void test() {
final DateTime begin = DateUtil.parse("2023-09-20");
final DateTime end = DateUtil.parse("2025-09-20");
final List<Date> dates = CronPatternUtil.matchedDates("0 0 1 3-3,9 *", begin, end, 20, false);
final List<Date> dates = CronPatternUtil.matchedDates("0 0 1 3-3,9 *", begin, end, 20);
//dates.forEach(Console::log);
Assertions.assertEquals(4, dates.size());
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2024. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.cron.pattern;
import org.dromara.hutool.core.date.DateTime;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.lang.Console;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.quartz.CronExpression;
import java.text.ParseException;
import java.util.Calendar;
public class IssueI9FQUATest {
@Test
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));
Assertions.assertEquals("2024-01-01 00:00:05", DateUtil.date(calendar).toString());
}
@Test
@Disabled
void createPatternBatchTest() throws ParseException {
final String cron = "0/5 * * * * ?";
final StopWatch stopWatch = new StopWatch();
stopWatch.start("Hutool");
CronPattern.of(cron);
stopWatch.stop();
stopWatch.start("Quartz");
new CronExpression(cron);
stopWatch.stop();
Console.log(stopWatch.prettyPrint());
}
@Test
@Disabled
void nextDateAfterBatchTest() throws ParseException {
final String cron = "0/5 * * * * ?";
final DateTime date = DateUtil.parse("2024-04-11 11:31:30");
final Calendar calendar = date.toCalendar();
final CronPattern cronPattern = CronPattern.of(cron);
final CronExpression expression = new CronExpression(cron);
final StopWatch stopWatch = new StopWatch();
stopWatch.start("Hutool");
cronPattern.nextMatchAfter(calendar);
stopWatch.stop();
stopWatch.start("Quartz");
expression.getNextValidTimeAfter(date);
stopWatch.stop();
Console.log(stopWatch.prettyPrint());
}
}