add method

This commit is contained in:
Looly 2022-02-13 19:40:40 +08:00
parent dc97eb7aca
commit 4fc615d224
3 changed files with 13 additions and 4 deletions

View File

@ -20,6 +20,7 @@
* 【core 】 新增copySafely方法与mkdirsSafely方法(pr#527@Gitee)
* 【core 】 新增MetroHash(pr#532@Gitee)
* 【core 】 SpringUtil增加publishEvent重载(pr#2139@Github)
* 【core 】 DateUtil增加rangeContains、rangeNotContains(pr#537@Gitee)
### 🐞Bug修复
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUGissue#2112@Github

View File

@ -1887,6 +1887,8 @@ public class DateUtil extends CalendarUtil {
* @param start 开始区间
* @param end 结束区间
* @return true 包含
* @author handy
* @since 5.7.21
*/
public static List<DateTime> rangeContains(DateRange start, DateRange end) {
List<DateTime> startDateTimes = CollUtil.newArrayList((Iterable<DateTime>) start);
@ -1900,6 +1902,8 @@ public class DateUtil extends CalendarUtil {
* @param start 开始区间
* @param end 结束区间
* @return true 包含
* @author handy
* @since 5.7.21
*/
public static List<DateTime> rangeNotContains(DateRange start, DateRange end) {
List<DateTime> startDateTimes = CollUtil.newArrayList((Iterable<DateTime>) start);

View File

@ -4,6 +4,7 @@ import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.junit.Assert;
import org.junit.Test;
@ -58,8 +59,8 @@ public class RangeTest {
Assert.assertEquals(sb.toString(), "1#2#3#");
StringBuilder sb2 = new StringBuilder();
DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb.append(DateTime.of(a).dayOfMonth()).append("#"));
Assert.assertEquals(sb2.toString(), "");
DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb2.append(DateTime.of(a).dayOfMonth()).append("#"));
Assert.assertEquals(sb2.toString(), StrUtil.EMPTY);
}
@Test
@ -149,7 +150,8 @@ public class RangeTest {
DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
// 交集
List<DateTime> dateTimes = DateUtil.rangeContains(startRange, endRange);
dateTimes.parallelStream().forEach(System.out::println);
Assert.assertEquals(1, dateTimes.size());
Assert.assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0));
}
@Test
@ -164,7 +166,9 @@ public class RangeTest {
DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
// 差集
List<DateTime> dateTimes1 = DateUtil.rangeNotContains(startRange, endRange);
dateTimes1.parallelStream().forEach(System.out::println);
Assert.assertEquals(1, dateTimes1.size());
Assert.assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0));
}
}