判断48小时内的俩个时间段是否重叠
程序员文章站
2022-05-14 21:33:34
...
public boolean dispatcherTimeVerify(List<DriverRewardBusinessRuleDTO> businessRuleDTOList) {
boolean overlapFlag = false;
for (int i = 0; i < businessRuleDTOList.size(); i++) {
String startTime = businessRuleDTOList.get(i).getDispatchTime().split("-")[0].trim();
String endTime = businessRuleDTOList.get(i).getDispatchTime().split("-")[1].trim();
String startTruckTimeType = businessRuleDTOList.get(i).getStartTruckTimeType().trim();
//跳过第一个时间段不做判断
if (businessRuleDTOList.size() - i > 1) {
for (int j = i + 1; j <= businessRuleDTOList.size() - 1; j++) {
if (!overlapFlag) {
String otherStartTime = businessRuleDTOList.get(j).getDispatchTime().split("-")[0].trim();
String otherEndTime = businessRuleDTOList.get(j).getDispatchTime().split("-")[1].trim();
String otherStartTruckTimeType = businessRuleDTOList.get(j).getStartTruckTimeType().trim();
// 调度时间不重叠且发车时间类型相同时
overlapFlag = isOverlap(startTime, endTime, otherStartTime, otherEndTime) && StringUtils.equals(startTruckTimeType, otherStartTruckTimeType);
}
}
}
}
return overlapFlag;
}
/**
* 判断俩个时间段是否重叠 重叠为true 不重叠为false
*/
private boolean isOverlap(String startTime, String endTime, String otherStartTime, String otherEndTime) {
return StringUtils.compare(startTime, otherEndTime) <= 0 && StringUtils.compare(endTime, otherStartTime) >= 0;
}
@Test
public void testDeleteTestData() {
// [{"dispatchTime":"00:00 - 01:01","startTruckTime":"00:01","startTruckTimeType":"0"},{"dispatchTime":"00:00 - 01:01","startTruckTime":"00:01","startTruckTimeType":"0"}]
List<DriverRewardBusinessRuleDTO> list = Lists.newArrayList();
DriverRewardBusinessRuleDTO dto = new DriverRewardBusinessRuleDTO();
DriverRewardBusinessRuleDTO dto1 = new DriverRewardBusinessRuleDTO();
dto.setDispatchTime("00:00 - 01:01");
dto1.setDispatchTime("01:00 - 01:01");
dto.setStartTruckTimeType("0");
dto1.setStartTruckTimeType("0");
list.add(dto);
list.add(dto1);
boolean b = driverRewardRuleService.dispatcherTimeVerify(list);
System.out.println(b);
}