mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix code
This commit is contained in:
parent
f23ec7eb16
commit
f5a44bca73
@ -74,7 +74,7 @@ public enum Week {
|
||||
/**
|
||||
* 获得星期对应{@link Calendar} 中的Week值
|
||||
*
|
||||
* @return 星期对应{@link Calendar} 中的Week值
|
||||
* @return 星期对应 {@link Calendar} 中的Week值
|
||||
*/
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.date.CalendarUtil;
|
||||
import cn.hutool.cron.pattern.matcher.PatternMatcher;
|
||||
import cn.hutool.cron.pattern.parser.PatternParser;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.GregorianCalendar;
|
||||
@ -129,6 +130,18 @@ public class CronPattern {
|
||||
return match(PatternUtil.getFields(calendar, isMatchSecond));
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定时间是否匹配定时任务表达式
|
||||
*
|
||||
* @param dateTime 时间
|
||||
* @param isMatchSecond 是否匹配秒
|
||||
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public boolean match(LocalDateTime dateTime, boolean isMatchSecond) {
|
||||
return match(PatternUtil.getFields(dateTime, isMatchSecond));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回匹配到的下一个时间
|
||||
*
|
||||
@ -137,7 +150,7 @@ public class CronPattern {
|
||||
*/
|
||||
public Calendar nextMatchAfter(Calendar calendar) {
|
||||
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 = CalendarUtil.beginOfDay(next);
|
||||
return nextMatchAfter(next);
|
||||
|
@ -1,5 +1,8 @@
|
||||
package cn.hutool.cron.pattern;
|
||||
|
||||
import cn.hutool.core.date.Week;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
|
||||
/**
|
||||
@ -10,6 +13,26 @@ import java.util.Calendar;
|
||||
*/
|
||||
class PatternUtil {
|
||||
|
||||
/**
|
||||
* 获取处理后的字段列表<br>
|
||||
* 月份从1开始,周从0开始
|
||||
*
|
||||
* @param dateTime {@link Calendar}
|
||||
* @param isMatchSecond 是否匹配秒,{@link false}则秒返回-1
|
||||
* @return 字段值列表
|
||||
* @since 5.8.0
|
||||
*/
|
||||
static int[] getFields(LocalDateTime dateTime, boolean isMatchSecond) {
|
||||
final int second = isMatchSecond ? dateTime.getSecond() : -1;
|
||||
final int minute = dateTime.getMinute();
|
||||
final int hour = dateTime.getHour();
|
||||
final int dayOfMonth = dateTime.getDayOfMonth();
|
||||
final int month = dateTime.getMonthValue();// 月份从1开始
|
||||
final int dayOfWeek = Week.of(dateTime.getDayOfWeek()).getValue() - 1; // 星期从0开始,0和7都表示周日
|
||||
final int year = dateTime.getYear();
|
||||
return new int[]{second, minute, hour, dayOfMonth, month, dayOfWeek, year};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取处理后的字段列表<br>
|
||||
* 月份从1开始,周从0开始
|
||||
|
@ -72,6 +72,17 @@ public class PatternMatcher {
|
||||
return match(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定周的值是否匹配定时任务表达式对应部分
|
||||
*
|
||||
* @param dayOfWeekValue dayOfMonth值,星期从0开始,0和7都表示周日
|
||||
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public boolean matchWeek(int dayOfWeekValue) {
|
||||
return matchers[5].match(dayOfWeekValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定时间是否匹配定时任务表达式
|
||||
*
|
||||
@ -88,7 +99,7 @@ public class PatternMatcher {
|
||||
return ((second < 0) || matchers[0].match(second)) // 匹配秒(非秒匹配模式下始终返回true)
|
||||
&& matchers[1].match(minute)// 匹配分
|
||||
&& matchers[2].match(hour)// 匹配时
|
||||
&& isMatchDayOfMonth(matchers[3], dayOfMonth, month, Year.isLeap(year))// 匹配日
|
||||
&& matchDayOfMonth(matchers[3], dayOfMonth, month, Year.isLeap(year))// 匹配日
|
||||
&& matchers[4].match(month) // 匹配月
|
||||
&& matchers[5].match(dayOfWeek)// 匹配周
|
||||
&& matchers[6].match(year);// 匹配年
|
||||
@ -103,7 +114,7 @@ public class PatternMatcher {
|
||||
* @param isLeapYear 是否闰年
|
||||
* @return 是否匹配
|
||||
*/
|
||||
private static boolean isMatchDayOfMonth(PartMatcher matcher, int dayOfMonth, int month, boolean isLeapYear) {
|
||||
private static boolean matchDayOfMonth(PartMatcher matcher, int dayOfMonth, int month, boolean isLeapYear) {
|
||||
return ((matcher instanceof DayOfMonthMatcher) //
|
||||
? ((DayOfMonthMatcher) matcher).match(dayOfMonth, month, isLeapYear) //
|
||||
: matcher.match(dayOfMonth));
|
||||
|
Loading…
Reference in New Issue
Block a user