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;
}
亲测可用 纯手工敲码 有问题联系 谢谢!