Merge pull request #2589 from fengbaoheng/v5-dev

扩展LocalDateTimeUtil.isIn方法使用场景
This commit is contained in:
Golden Looly 2022-09-04 21:55:56 +08:00 committed by GitHub
commit c0cf8cf42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 3 deletions

View File

@ -598,6 +598,20 @@ public class LocalDateTimeUtil {
return date1 != null && date2 != null && date1.isEqual(date2);
}
/**
* 判断当前时间默认时区是否在指定范围内<br>
* 起始时间和结束时间可以互换
*
* @param beginDate 起始时间包含
* @param endDate 结束时间包含
* @return 是否在范围内
* @author FengBaoheng
* @since 5.8.6
*/
public static boolean isIn(ChronoLocalDateTime<?> beginDate, ChronoLocalDateTime<?> endDate) {
return TemporalAccessorUtil.isIn(LocalDateTimeUtil.now(), beginDate, endDate);
}
/**
* 当前日期是否在日期指定范围内<br>
* 起始日期和结束日期可以互换
@ -608,7 +622,26 @@ public class LocalDateTimeUtil {
* @return 是否在范围内
* @since 5.8.5
*/
public static boolean isIn(ChronoLocalDateTime<?> date, ChronoLocalDateTime<?> beginDate, ChronoLocalDateTime<?> endDate){
public static boolean isIn(ChronoLocalDateTime<?> date, ChronoLocalDateTime<?> beginDate, ChronoLocalDateTime<?> endDate) {
return TemporalAccessorUtil.isIn(date, beginDate, endDate);
}
/**
* 判断当前时间默认时区是否在指定范围内<br>
* 起始时间和结束时间可以互换<br>
* 通过includeBegin, includeEnd参数控制时间范围区间是否为开区间例如传入参数includeBegin=true, includeEnd=false
* 则本方法会判断 date (beginDate, endDate] 是否成立
*
* @param beginDate 起始时间包含
* @param endDate 结束时间包含
* @param includeBegin 时间范围是否包含起始时间
* @param includeEnd 时间范围是否包含结束时间
* @return 是否在范围内
* @author FengBaoheng
* @since 5.8.6
*/
public static boolean isIn(ChronoLocalDateTime<?> date, ChronoLocalDateTime<?> beginDate,
ChronoLocalDateTime<?> endDate, boolean includeBegin, boolean includeEnd) {
return TemporalAccessorUtil.isIn(date, beginDate, endDate, includeBegin, includeEnd);
}
}

View File

@ -180,11 +180,49 @@ public class TemporalAccessorUtil extends TemporalUtil{
* @return 是否在范围内
* @since 5.8.5
*/
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate){
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate) {
return isIn(date, beginDate, endDate, true, true);
}
/**
* 当前日期是否在日期指定范围内<br>
* 起始日期和结束日期可以互换<br>
* 通过includeBegin, includeEnd参数控制日期范围区间是否为开区间例如传入参数includeBegin=true, includeEnd=false
* 则本方法会判断 date (beginDate, endDate] 是否成立
*
* @param date 被检查的日期
* @param beginDate 起始日期
* @param endDate 结束日期
* @param includeBegin 时间范围是否包含起始日期
* @param includeEnd 时间范围是否包含结束日期
* @return 是否在范围内
* @author FengBaoheng
* @since 5.8.6
*/
public static boolean isIn(TemporalAccessor date, TemporalAccessor beginDate, TemporalAccessor endDate,
boolean includeBegin, boolean includeEnd) {
if (date == null || beginDate == null || endDate == null) {
throw new IllegalArgumentException("参数不可为null");
}
final long thisMills = toEpochMilli(date);
final long beginMills = toEpochMilli(beginDate);
final long endMills = toEpochMilli(endDate);
final long rangeMin = Math.min(beginMills, endMills);
final long rangeMax = Math.max(beginMills, endMills);
return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
// 先判断是否满足 date (beginDate, endDate)
boolean isIn = rangeMin < thisMills && thisMills < rangeMax;
// 若不满足则再判断是否在时间范围的边界上
if (!isIn && includeBegin) {
isIn = thisMills == rangeMin;
}
if (!isIn && includeEnd) {
isIn = thisMills == rangeMax;
}
return isIn;
}
}

View File

@ -139,6 +139,47 @@ public class LocalDateTimeUtilTest {
Assert.assertEquals(365, between.toDays());
}
@Test
public void isIn() {
// 时间范围 8点-9点
LocalDateTime begin = LocalDateTime.parse("2019-02-02T08:00:00");
LocalDateTime end = LocalDateTime.parse("2019-02-02T09:00:00");
// 不在时间范围内 用例
Assert.assertFalse(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T06:00:00"), begin, end));
Assert.assertFalse(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T13:00:00"), begin, end));
Assert.assertFalse(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-01T08:00:00"), begin, end));
Assert.assertFalse(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-03T09:00:00"), begin, end));
// 在时间范围内 用例
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:00:00"), begin, end));
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:00:01"), begin, end));
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:11:00"), begin, end));
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:22:00"), begin, end));
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:59:59"), begin, end));
Assert.assertTrue(LocalDateTimeUtil.isIn(LocalDateTime.parse("2019-02-02T09:00:00"), begin, end));
// 测试边界条件
Assert.assertTrue(LocalDateTimeUtil.isIn(begin, begin, end, true, false));
Assert.assertFalse(LocalDateTimeUtil.isIn(begin, begin, end, false, false));
Assert.assertTrue(LocalDateTimeUtil.isIn(end, begin, end, false, true));
Assert.assertFalse(LocalDateTimeUtil.isIn(end, begin, end, false, false));
// beginend互换
Assert.assertTrue(LocalDateTimeUtil.isIn(begin, end, begin, true, true));
// 比较当前时间范围
LocalDateTime now = LocalDateTime.now();
Assert.assertTrue(LocalDateTimeUtil.isIn(now.minusHours(1L), now.plusHours(1L)));
Assert.assertFalse(LocalDateTimeUtil.isIn(now.minusHours(1L), now.minusHours(2L)));
Assert.assertFalse(LocalDateTimeUtil.isIn(now.plusHours(1L), now.plusHours(2L)));
// 异常入参
Assert.assertThrows(IllegalArgumentException.class, () -> LocalDateTimeUtil.isIn(null, begin, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, () -> LocalDateTimeUtil.isIn(begin, null, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, () -> LocalDateTimeUtil.isIn(begin, begin, null, false, false));
}
@Test
public void beginOfDayTest() {
final LocalDateTime localDateTime = LocalDateTimeUtil.parse("2020-01-23T12:23:56");