mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
2722a971af
commit
77f4d529fa
@ -416,11 +416,7 @@ public class LocalDateTimeUtil {
|
|||||||
* @return 偏移后的日期时间
|
* @return 偏移后的日期时间
|
||||||
*/
|
*/
|
||||||
public static LocalDateTime offset(LocalDateTime time, long number, TemporalUnit field) {
|
public static LocalDateTime offset(LocalDateTime time, long number, TemporalUnit field) {
|
||||||
if (null == time) {
|
return TemporalUtil.offset(time, number, field);
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return time.plus(number, field);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
package cn.hutool.core.date;
|
package cn.hutool.core.date;
|
||||||
|
|
||||||
|
import java.time.DayOfWeek;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.time.temporal.Temporal;
|
import java.time.temporal.Temporal;
|
||||||
|
import java.time.temporal.TemporalAdjusters;
|
||||||
|
import java.time.temporal.TemporalUnit;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,4 +105,37 @@ public class TemporalUtil {
|
|||||||
throw new IllegalArgumentException("ChronoUnit cannot be converted to TimeUnit: " + unit);
|
throw new IllegalArgumentException("ChronoUnit cannot be converted to TimeUnit: " + unit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日期偏移,根据field不同加不同值(偏移会修改传入的对象)
|
||||||
|
*
|
||||||
|
* @param <T> 日期类型,如LocalDate或LocalDateTime
|
||||||
|
* @param time {@link Temporal}
|
||||||
|
* @param number 偏移量,正数为向后偏移,负数为向前偏移
|
||||||
|
* @param field 偏移单位,见{@link ChronoUnit},不能为null
|
||||||
|
* @return 偏移后的日期时间
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T extends Temporal> T offset(T time, long number, TemporalUnit field) {
|
||||||
|
if (null == time) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (T) time.plus(number, field);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 偏移到指定的周几
|
||||||
|
*
|
||||||
|
* @param temporal 日期或者日期时间
|
||||||
|
* @param dayOfWeek 周几
|
||||||
|
* @param <T> 日期类型,如LocalDate或LocalDateTime
|
||||||
|
* @param isPrevious 是否向前偏移,{@code true}向前偏移,{@code false}向后偏移。
|
||||||
|
* @return 偏移后的日期
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T extends Temporal> T offset(T temporal, DayOfWeek dayOfWeek, boolean isPrevious) {
|
||||||
|
return (T) temporal.with(isPrevious ? TemporalAdjusters.previous(dayOfWeek) : TemporalAdjusters.next(dayOfWeek));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,7 @@ public class CronPattern {
|
|||||||
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
||||||
*/
|
*/
|
||||||
public boolean match(Calendar calendar, boolean isMatchSecond) {
|
public boolean match(Calendar calendar, boolean isMatchSecond) {
|
||||||
final int[] fields = getFields(calendar, isMatchSecond);
|
return match(PatternUtil.getFields(calendar, isMatchSecond));
|
||||||
return match(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,7 +136,7 @@ public class CronPattern {
|
|||||||
* @return 匹配到的下一个时间
|
* @return 匹配到的下一个时间
|
||||||
*/
|
*/
|
||||||
public Calendar nextMatchAfter(Calendar calendar) {
|
public Calendar nextMatchAfter(Calendar calendar) {
|
||||||
return nextMatchAfter(getFields(calendar, true), calendar.getTimeZone());
|
return nextMatchAfter(PatternUtil.getFields(calendar, true), calendar.getTimeZone());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -148,18 +147,12 @@ public class CronPattern {
|
|||||||
/**
|
/**
|
||||||
* 给定时间是否匹配定时任务表达式
|
* 给定时间是否匹配定时任务表达式
|
||||||
*
|
*
|
||||||
* @param second 秒数,-1表示不匹配此项
|
* @param fields 时间字段值,{second, minute, hour, dayOfMonth, month, dayOfWeek, year}
|
||||||
* @param minute 分钟
|
|
||||||
* @param hour 小时
|
|
||||||
* @param dayOfMonth 天
|
|
||||||
* @param month 月,从1开始
|
|
||||||
* @param dayOfWeek 周,从0开始,0和7都表示周日
|
|
||||||
* @param year 年
|
|
||||||
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
* @return 如果匹配返回 {@code true}, 否则返回 {@code false}
|
||||||
*/
|
*/
|
||||||
private boolean match(int second, int minute, int hour, int dayOfMonth, int month, int dayOfWeek, int year) {
|
private boolean match(int[] fields) {
|
||||||
for (PatternMatcher matcher : matchers) {
|
for (PatternMatcher matcher : matchers) {
|
||||||
if (matcher.match(second, minute, hour, dayOfMonth, month, dayOfWeek, year)) {
|
if (matcher.match(fields[0], fields[1], fields[2], fields[3], fields[4], fields[5], fields[6])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -181,24 +174,4 @@ public class CronPattern {
|
|||||||
// 返回匹配到的最早日期
|
// 返回匹配到的最早日期
|
||||||
return CollUtil.min(nextMatches);
|
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};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
32
hutool-cron/src/main/java/cn/hutool/cron/pattern/PatternUtil.java
Executable file
32
hutool-cron/src/main/java/cn/hutool/cron/pattern/PatternUtil.java
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
package cn.hutool.cron.pattern;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表达式工具,内部使用
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
class PatternUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取处理后的字段列表<br>
|
||||||
|
* 月份从1开始,周从0开始
|
||||||
|
*
|
||||||
|
* @param calendar {@link Calendar}
|
||||||
|
* @param isMatchSecond 是否匹配秒,{@link false}则秒返回-1
|
||||||
|
* @return 字段值列表
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
static 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…
x
Reference in New Issue
Block a user