0
点赞
收藏
分享

微信扫一扫

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


场景

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

思想是:

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

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

注:

实现

/***
*
* @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;
}
}

 

举报

相关推荐

0 条评论