This commit is contained in:
Looly 2022-09-04 21:29:29 +08:00
parent c134534bf2
commit 7fb7667e14
4 changed files with 129 additions and 16 deletions

View File

@ -783,11 +783,7 @@ public class DateTime extends Date {
* @since 3.0.8
*/
public boolean isIn(final Date beginDate, final Date endDate) {
final long beginMills = beginDate.getTime();
final long endMills = endDate.getTime();
final long thisMills = this.getTime();
return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
return DateUtil.isIn(this, beginDate, endDate);
}
/**

View File

@ -1381,11 +1381,49 @@ public class DateUtil extends CalendarUtil {
* @since 3.0.8
*/
public static boolean isIn(final Date date, final Date beginDate, final Date endDate) {
if (date instanceof DateTime) {
return ((DateTime) date).isIn(beginDate, endDate);
} else {
return new DateTime(date).isIn(beginDate, 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(final Date date, final Date beginDate, final Date endDate,
final boolean includeBegin, final boolean includeEnd) {
if (date == null || beginDate == null || endDate == null) {
throw new IllegalArgumentException("参数不可为null");
}
final long thisMills = date.getTime();
final long beginMills = beginDate.getTime();
final long endMills = endDate.getTime();
final long rangeMin = Math.min(beginMills, endMills);
final long rangeMax = Math.max(beginMills, endMills);
// 先判断是否满足 date (beginDate, endDate)
boolean isIn = rangeMin < thisMills && thisMills < rangeMax;
// 若不满足则再判断是否在时间范围的边界上
if (false == isIn && includeBegin) {
isIn = thisMills == rangeMin;
}
if (false == isIn && includeEnd) {
isIn = thisMills == rangeMax;
}
return isIn;
}
/**

View File

@ -181,11 +181,49 @@ public class TemporalAccessorUtil extends TemporalUtil{
* @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);
public static boolean isIn(final TemporalAccessor date, final TemporalAccessor beginDate, final TemporalAccessor endDate){
return isIn(date, beginDate, endDate, true, true);
}
return thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);
/**
* 当前日期是否在日期指定范围内<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(final TemporalAccessor date, final TemporalAccessor beginDate, final TemporalAccessor endDate,
final boolean includeBegin, final 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);
// 先判断是否满足 date (beginDate, endDate)
boolean isIn = rangeMin < thisMills && thisMills < rangeMax;
// 若不满足则再判断是否在时间范围的边界上
if (false == isIn && includeBegin) {
isIn = thisMills == rangeMin;
}
if (false == isIn && includeEnd) {
isIn = thisMills == rangeMax;
}
return isIn;
}
}

View File

@ -10,7 +10,6 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Objects;
public class TimeUtilTest {
@ -234,7 +233,49 @@ public class TimeUtilTest {
@Test
public void ofTest2(){
final Instant instant = Objects.requireNonNull(DateUtil.parse("2022-02-22")).toInstant();
final LocalDateTime of = TimeUtil.of((TemporalAccessor) instant);
final LocalDateTime of = TimeUtil.of(instant);
Console.log(of);
}
@SuppressWarnings("ConstantConditions")
@Test
public void isInTest() {
// 时间范围 8点-9点
final LocalDateTime begin = LocalDateTime.parse("2019-02-02T08:00:00");
final LocalDateTime end = LocalDateTime.parse("2019-02-02T09:00:00");
// 不在时间范围内 用例
Assert.assertFalse(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T06:00:00"), begin, end));
Assert.assertFalse(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T13:00:00"), begin, end));
Assert.assertFalse(TimeUtil.isIn(LocalDateTime.parse("2019-02-01T08:00:00"), begin, end));
Assert.assertFalse(TimeUtil.isIn(LocalDateTime.parse("2019-02-03T09:00:00"), begin, end));
// 在时间范围内 用例
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:00:00"), begin, end));
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:00:01"), begin, end));
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:11:00"), begin, end));
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:22:00"), begin, end));
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T08:59:59"), begin, end));
Assert.assertTrue(TimeUtil.isIn(LocalDateTime.parse("2019-02-02T09:00:00"), begin, end));
// 测试边界条件
Assert.assertTrue(TimeUtil.isIn(begin, begin, end, true, false));
Assert.assertFalse(TimeUtil.isIn(begin, begin, end, false, false));
Assert.assertTrue(TimeUtil.isIn(end, begin, end, false, true));
Assert.assertFalse(TimeUtil.isIn(end, begin, end, false, false));
// beginend互换
Assert.assertTrue(TimeUtil.isIn(begin, end, begin, true, true));
// 比较当前时间范围
final LocalDateTime now = LocalDateTime.now();
Assert.assertTrue(TimeUtil.isIn(now, now.minusHours(1L), now.plusHours(1L)));
Assert.assertFalse(TimeUtil.isIn(now, now.minusHours(1L), now.minusHours(2L)));
Assert.assertFalse(TimeUtil.isIn(now, now.plusHours(1L), now.plusHours(2L)));
// 异常入参
Assert.assertThrows(IllegalArgumentException.class, () -> TimeUtil.isIn(null, begin, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, () -> TimeUtil.isIn(begin, null, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, () -> TimeUtil.isIn(begin, begin, null, false, false));
}
}