場景
兩個時間段,判斷是否有交集。
思想是:
找到兩個時間段開始時間的最大值和結束時間的最小值。
如果開始時間的最大值小於等於結束時間的最小值則說明這兩個時間段有交集。
注:
博客:
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; } }