合并isBetween、isIn方法

This commit is contained in:
fengbaoheng
2022-09-04 01:07:15 +08:00
parent b906cad408
commit be9711d5dd
3 changed files with 97 additions and 100 deletions

View File

@@ -140,36 +140,39 @@ public class LocalDateTimeUtilTest {
}
@Test
public void isBetween() {
public void isIn() {
// 时间范围 8点-9点
LocalDateTime start = LocalDateTime.parse("2019-02-02T08:00:00");
LocalDateTime begin = LocalDateTime.parse("2019-02-02T08:00:00");
LocalDateTime end = LocalDateTime.parse("2019-02-02T09:00:00");
// 不在时间范围内 用例
Assert.assertFalse(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T06:00:00"), start, end));
Assert.assertFalse(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T13:00:00"), start, end));
Assert.assertFalse(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-01T08:00:00"), start, end));
Assert.assertFalse(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-03T09:00:00"), start, end));
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.isBetween(LocalDateTime.parse("2019-02-02T08:00:00"), start, end));
Assert.assertTrue(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T08:00:01"), start, end));
Assert.assertTrue(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T08:11:00"), start, end));
Assert.assertTrue(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T08:22:00"), start, end));
Assert.assertTrue(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T08:59:59"), start, end));
Assert.assertTrue(LocalDateTimeUtil.isBetween(LocalDateTime.parse("2019-02-02T09:00:00"), start, 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.isBetween(start, start, end, true, false));
Assert.assertFalse(LocalDateTimeUtil.isBetween(start, start, end, false, false));
Assert.assertTrue(LocalDateTimeUtil.isBetween(end, start, end, false, true));
Assert.assertFalse(LocalDateTimeUtil.isBetween(end, start, end, false, false));
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));
// begin、end互换
Assert.assertTrue(LocalDateTimeUtil.isIn(begin, end, begin, true, true));
// 异常入参
Assert.assertThrows(IllegalArgumentException.class, ()->LocalDateTimeUtil.isBetween(null, start, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, ()->LocalDateTimeUtil.isBetween(start, null, end, false, false));
Assert.assertThrows(IllegalArgumentException.class, ()->LocalDateTimeUtil.isBetween(start, start, null, false, false));
Assert.assertThrows(IllegalArgumentException.class, ()->LocalDateTimeUtil.isBetween(start, end, start, false, false));
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