mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
Merge branch 'v5-dev' of gitee.com:dromara/hutool into v5-dev
This commit is contained in:
commit
8c770d4e67
@ -125,15 +125,8 @@ public class CronPattern {
|
||||
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
||||
*/
|
||||
public boolean match(Calendar calendar, boolean isMatchSecond) {
|
||||
final int second = isMatchSecond ? calendar.get(Calendar.SECOND) : -1;
|
||||
final int minute = calendar.get(Calendar.MINUTE);
|
||||
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
final int month = calendar.get(Calendar.MONTH) + 1;// 月份从1开始
|
||||
final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 星期从0开始,0和7都表示周日
|
||||
final int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
return match(second, minute, hour, dayOfMonth, month, dayOfWeek, year);
|
||||
final int[] fields = getFields(calendar, isMatchSecond);
|
||||
return match(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,15 +137,7 @@ public class CronPattern {
|
||||
* @return 匹配到的下一个时间
|
||||
*/
|
||||
public Calendar nextMatchAfter(Calendar calendar) {
|
||||
final int second = calendar.get(Calendar.SECOND);
|
||||
final int minute = calendar.get(Calendar.MINUTE);
|
||||
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
final int month = calendar.get(Calendar.MONTH) + 1;// 月份从1开始
|
||||
final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 星期从0开始,0和7都表示周日
|
||||
final int year = calendar.get(Calendar.YEAR);
|
||||
|
||||
return nextMatchAfter(new int[]{second, minute, hour, dayOfMonth, month, dayOfWeek, year}, calendar.getTimeZone());
|
||||
return nextMatchAfter(getFields(calendar, true), calendar.getTimeZone());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -184,8 +169,8 @@ public class CronPattern {
|
||||
/**
|
||||
* 获取下一个最近的匹配日期时间
|
||||
*
|
||||
* @param values 时间字段值
|
||||
* @param zone 时区
|
||||
* @param values 时间字段值
|
||||
* @param zone 时区
|
||||
* @return {@link Calendar}
|
||||
*/
|
||||
private Calendar nextMatchAfter(int[] values, TimeZone zone) {
|
||||
@ -196,4 +181,24 @@ public class CronPattern {
|
||||
// 返回匹配到的最早日期
|
||||
return CollUtil.min(nextMatches);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取处理后的字段列表<br>
|
||||
* 月份从1开始,周从0开始
|
||||
*
|
||||
* @param calendar {@link Calendar}
|
||||
* @param isMatchSecond 是否匹配秒,{@link false}则秒返回-1
|
||||
* @return 字段值列表
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private int[] getFields(Calendar calendar, boolean isMatchSecond) {
|
||||
final int second = isMatchSecond ? calendar.get(Calendar.SECOND) : -1;
|
||||
final int minute = calendar.get(Calendar.MINUTE);
|
||||
final int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||
final int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
||||
final int month = calendar.get(Calendar.MONTH) + 1;// 月份从1开始
|
||||
final int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 星期从0开始,0和7都表示周日
|
||||
final int year = calendar.get(Calendar.YEAR);
|
||||
return new int[]{second, minute, hour, dayOfMonth, month, dayOfWeek, year};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user