Java中判断两个Date时间段是否有交集的方法


场景

两个时间段,判断是否有交集。

思想是:

找到两个时间段开始时间的最大值和结束时间的最小值。

如果开始时间的最大值小于等于结束时间的最小值则说明这两个时间段有交集。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

    /***
     * 
     * @param startDateOne 第一个时间段的开始时间
     * @param endDateOne 第一个时间段的结束时间
     * @param startDateTwo 第二个时间段的开始时间
     * @param endDateTwo 第二个时间段的结束时间
     * @return
     */
    public static Boolean IsInterSection(Date startDateOne,Date endDateOne,Date startDateTwo,Date endDateTwo)
    {
        Date maxStartDate = startDateOne;
        if(maxStartDate.before(startDateTwo))
        {
            maxStartDate = startDateTwo;
        }

        Date minEndDate = endDateOne;
        if(endDateTwo.before(minEndDate))
        {
            minEndDate = endDateTwo;
        }
        if(maxStartDate.before(minEndDate) || (maxStartDate.getTime() == minEndDate.getTime()))
        {
            return true;
        }
        else {
            return  false;
        }
    } 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM