mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add isIn
This commit is contained in:
parent
ced4e50334
commit
df9050026e
@ -162,4 +162,21 @@ public class TemporalAccessorUtil extends TemporalUtil{
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当前日期是否在日期指定范围内<br>
|
||||||
|
* 起始日期和结束日期可以互换
|
||||||
|
*
|
||||||
|
* @param date 被检查的日期
|
||||||
|
* @param beginDate 起始日期(包含)
|
||||||
|
* @param endDate 结束日期(包含)
|
||||||
|
* @return 是否在范围内
|
||||||
|
*/
|
||||||
|
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate){
|
||||||
|
final long thisMills = TemporalAccessorUtil.toEpochMilli(date);
|
||||||
|
final long beginMills = TemporalAccessorUtil.toEpochMilli(beginDate);
|
||||||
|
final long endMills = TemporalAccessorUtil.toEpochMilli(endDate);
|
||||||
|
|
||||||
|
return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import java.time.LocalTime;
|
|||||||
import java.time.Period;
|
import java.time.Period;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
|
import java.time.chrono.ChronoLocalDate;
|
||||||
import java.time.chrono.ChronoLocalDateTime;
|
import java.time.chrono.ChronoLocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.format.DateTimeFormatterBuilder;
|
import java.time.format.DateTimeFormatterBuilder;
|
||||||
@ -34,7 +35,7 @@ import java.util.TimeZone;
|
|||||||
* @see DatePattern 常用格式工具类
|
* @see DatePattern 常用格式工具类
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public class TimeUtil {
|
public class TimeUtil extends TemporalAccessorUtil{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当前时间,默认时区
|
* 当前时间,默认时区
|
||||||
@ -54,16 +55,6 @@ public class TimeUtil {
|
|||||||
return LocalDate.now();
|
return LocalDate.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link Instant}转{@link LocalDateTime},使用默认时区
|
|
||||||
*
|
|
||||||
* @param instant {@link Instant}
|
|
||||||
* @return {@link LocalDateTime}
|
|
||||||
*/
|
|
||||||
public static LocalDateTime of(final Instant instant) {
|
|
||||||
return of(instant, ZoneId.systemDefault());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Instant}转{@link LocalDateTime},使用UTC时区
|
* {@link Instant}转{@link LocalDateTime},使用UTC时区
|
||||||
*
|
*
|
||||||
@ -74,19 +65,6 @@ public class TimeUtil {
|
|||||||
return of(instant, ZoneId.of("UTC"));
|
return of(instant, ZoneId.of("UTC"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link ZonedDateTime}转{@link LocalDateTime}
|
|
||||||
*
|
|
||||||
* @param zonedDateTime {@link ZonedDateTime}
|
|
||||||
* @return {@link LocalDateTime}
|
|
||||||
*/
|
|
||||||
public static LocalDateTime of(final ZonedDateTime zonedDateTime) {
|
|
||||||
if (null == zonedDateTime) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return zonedDateTime.toLocalDateTime();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Instant}转{@link LocalDateTime}
|
* {@link Instant}转{@link LocalDateTime}
|
||||||
*
|
*
|
||||||
@ -193,6 +171,8 @@ public class TimeUtil {
|
|||||||
return ((LocalDate) temporalAccessor).atStartOfDay();
|
return ((LocalDate) temporalAccessor).atStartOfDay();
|
||||||
} else if (temporalAccessor instanceof Instant) {
|
} else if (temporalAccessor instanceof Instant) {
|
||||||
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
||||||
|
} else if(temporalAccessor instanceof ZonedDateTime){
|
||||||
|
return ((ZonedDateTime)temporalAccessor).toLocalDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
return LocalDateTime.of(
|
return LocalDateTime.of(
|
||||||
@ -359,32 +339,10 @@ public class TimeUtil {
|
|||||||
* @return 格式化后的字符串
|
* @return 格式化后的字符串
|
||||||
* @since 5.3.11
|
* @since 5.3.11
|
||||||
*/
|
*/
|
||||||
public static String formatNormal(final LocalDateTime time) {
|
public static String formatNormal(final ChronoLocalDateTime<?> time) {
|
||||||
return format(time, DatePattern.NORM_DATETIME_FORMATTER);
|
return format(time, DatePattern.NORM_DATETIME_FORMATTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化日期时间为指定格式
|
|
||||||
*
|
|
||||||
* @param time {@link LocalDateTime}
|
|
||||||
* @param formatter 日期格式化器,预定义的格式见:{@link DateTimeFormatter}
|
|
||||||
* @return 格式化后的字符串
|
|
||||||
*/
|
|
||||||
public static String format(final LocalDateTime time, final DateTimeFormatter formatter) {
|
|
||||||
return TemporalAccessorUtil.format(time, formatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化日期时间为指定格式
|
|
||||||
*
|
|
||||||
* @param time {@link LocalDateTime}
|
|
||||||
* @param format 日期格式,类似于yyyy-MM-dd HH:mm:ss,SSS
|
|
||||||
* @return 格式化后的字符串
|
|
||||||
*/
|
|
||||||
public static String format(final LocalDateTime time, final String format) {
|
|
||||||
return TemporalAccessorUtil.format(time, format);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化日期时间为yyyy-MM-dd格式
|
* 格式化日期时间为yyyy-MM-dd格式
|
||||||
*
|
*
|
||||||
@ -392,37 +350,10 @@ public class TimeUtil {
|
|||||||
* @return 格式化后的字符串
|
* @return 格式化后的字符串
|
||||||
* @since 5.3.11
|
* @since 5.3.11
|
||||||
*/
|
*/
|
||||||
public static String formatNormal(final LocalDate date) {
|
public static String formatNormal(final ChronoLocalDate date) {
|
||||||
return format(date, DatePattern.NORM_DATE_FORMATTER);
|
return format(date, DatePattern.NORM_DATE_FORMATTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化日期时间为指定格式
|
|
||||||
*
|
|
||||||
* @param date {@link LocalDate}
|
|
||||||
* @param formatter 日期格式化器,预定义的格式见:{@link DateTimeFormatter}; 常量如: {@link DatePattern#NORM_DATE_FORMATTER}, {@link DatePattern#NORM_DATETIME_FORMATTER}
|
|
||||||
* @return 格式化后的字符串
|
|
||||||
* @since 5.3.10
|
|
||||||
*/
|
|
||||||
public static String format(final LocalDate date, final DateTimeFormatter formatter) {
|
|
||||||
return TemporalAccessorUtil.format(date, formatter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 格式化日期时间为指定格式
|
|
||||||
*
|
|
||||||
* @param date {@link LocalDate}
|
|
||||||
* @param format 日期格式,类似于yyyy-MM-dd, 常量如 {@link DatePattern#NORM_DATE_PATTERN}, {@link DatePattern#NORM_DATETIME_PATTERN}
|
|
||||||
* @return 格式化后的字符串
|
|
||||||
* @since 5.3.10
|
|
||||||
*/
|
|
||||||
public static String format(final LocalDate date, final String format) {
|
|
||||||
if (null == date) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return format(date, DateTimeFormatter.ofPattern(format));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日期偏移,根据field不同加不同值(偏移会修改传入的对象)
|
* 日期偏移,根据field不同加不同值(偏移会修改传入的对象)
|
||||||
*
|
*
|
||||||
@ -517,18 +448,6 @@ public class TimeUtil {
|
|||||||
return time.with(LocalTime.MAX);
|
return time.with(LocalTime.MAX);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link TemporalAccessor}转换为 时间戳(从1970-01-01T00:00:00Z开始的毫秒数)
|
|
||||||
*
|
|
||||||
* @param temporalAccessor Date对象
|
|
||||||
* @return {@link Instant}对象
|
|
||||||
* @see TemporalAccessorUtil#toEpochMilli(TemporalAccessor)
|
|
||||||
* @since 5.4.1
|
|
||||||
*/
|
|
||||||
public static long toEpochMilli(final TemporalAccessor temporalAccessor) {
|
|
||||||
return TemporalAccessorUtil.toEpochMilli(temporalAccessor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否为周末(周六或周日)
|
* 是否为周末(周六或周日)
|
||||||
*
|
*
|
||||||
@ -608,7 +527,7 @@ public class TimeUtil {
|
|||||||
* @return 是否为同一天
|
* @return 是否为同一天
|
||||||
* @since 5.8.5
|
* @since 5.8.5
|
||||||
*/
|
*/
|
||||||
public static boolean isSameDay(final LocalDateTime date1, final LocalDateTime date2) {
|
public static boolean isSameDay(final ChronoLocalDateTime<?> date1, final ChronoLocalDateTime<?> date2) {
|
||||||
return date1 != null && date2 != null && isSameDay(date1.toLocalDate(), date2.toLocalDate());
|
return date1 != null && date2 != null && isSameDay(date1.toLocalDate(), date2.toLocalDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -620,7 +539,7 @@ public class TimeUtil {
|
|||||||
* @return 是否为同一天
|
* @return 是否为同一天
|
||||||
* @since 5.8.5
|
* @since 5.8.5
|
||||||
*/
|
*/
|
||||||
public static boolean isSameDay(final LocalDate date1, final LocalDate date2) {
|
public static boolean isSameDay(final ChronoLocalDate date1, final ChronoLocalDate date2) {
|
||||||
return date1 != null && date2 != null && date1.isEqual(date2);
|
return date1 != null && date2 != null && date1.isEqual(date2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,16 @@ public class TemporalAccessorUtilTest {
|
|||||||
LocalDate.of(2021, 6, 26), "#SSS");
|
LocalDate.of(2021, 6, 26), "#SSS");
|
||||||
Assert.assertEquals("1624636800000", today2);
|
Assert.assertEquals("1624636800000", today2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isInTest(){
|
||||||
|
final String sourceStr = "2022-04-19 00:00:00";
|
||||||
|
final String startTimeStr = "2022-04-19 00:00:00";
|
||||||
|
final String endTimeStr = "2022-04-19 23:59:59";
|
||||||
|
final boolean between = TimeUtil.isIn(
|
||||||
|
TimeUtil.parse(sourceStr, DatePattern.NORM_DATETIME_FORMATTER),
|
||||||
|
TimeUtil.parse(startTimeStr, DatePattern.NORM_DATETIME_FORMATTER),
|
||||||
|
TimeUtil.parse(endTimeStr, DatePattern.NORM_DATETIME_FORMATTER));
|
||||||
|
Assert.assertTrue(between);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user