Data两个时间段区间是否含有交集工具类

public static void main(String[] args) {
        //{2021-05-10  2021-05-20}{2021-05-01  2021-05-11}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1619798400000L), new Date(1620662400000L)));
        //{2021-05-10  2021-05-20}{2021-05-19  2021-05-22}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1621353600000L), new Date(1621612800000L)));
        //{2021-05-10  2021-05-20}{2021-05-12  2021-05-15}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1620748800000L), new Date(1621008000000L)));
        //{2021-05-10  2021-05-20}{2021-05-01  2021-05-28}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1619798400000L), new Date(1622131200000L)));

        //{2021-05-10  2021-05-20}{2021-05-01  2021-05-09}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1619798400000L), new Date(1620489600000L)));
        //{2021-05-10  2021-05-20}{2021-05-21  2021-05-28}
        System.out.println(intersectionVerify(new Date(1620576000000L), new Date(1621440000000L), new Date(1621526400000L), new Date(1622131200000L)));


    }

    /**
     * 验证时间区间是否 还有交集    10日-20日  可能交集时间段 1-11  19-22  12-15  1-28
     * <p>
     * 获取两个时间段开始时间的最大值和结束时间的最小值
     * 如果开始时间的最大值小于等于结束时间的最小值则说明这两个时间段有交集
     *
     * @param startDateOne 开始区间-1
     * @param endDateOne   结束区间-1
     * @param startDateTwo 开始区间-2
     * @param endDateTwo   结束区间-2
     * @return
     */
    public static Boolean intersectionVerify(Date startDateOne, Date endDateOne, Date startDateTwo, Date endDateTwo) {
        //最大开始时间
        Date maxStartDate = startDateOne;
        if (maxStartDate.before(startDateTwo)) {
            maxStartDate = startDateTwo;
        }// 1区间开始时间 是否在 2区间开始时间之前   获取最大的开始时间
        //最小结束时间
        Date minEndDate = endDateOne;
        if (minEndDate.after(endDateTwo)) {
            minEndDate = endDateTwo;
        }  //1区间结束时间 是否在 2区间结束时间之后  获取最小结束时间

        // 开始时间是否在结束时间之前     时间戳比较时间是否相等
        if (maxStartDate.before(minEndDate) || (maxStartDate.getTime() == minEndDate.getTime())) {
            return true;
        }
        return false;
    }

亲测可用 纯手工敲码 有问题联系 谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值