首页 > 编程语言 > 详细

java计算两个时间段是否重合

时间:2021-06-30 18:08:45      阅读:13      评论:0      收藏:0      [点我收藏+]

public static boolean intersection(LocalTime startTime,LocalTime endTime , LocalTime startTimeNew ,LocalTime endTimeNew){


if(endTimeNew.compareTo(startTime) >= 0 && startTimeNew.compareTo(startTime) <= 0){
return true;
}
if(endTime.compareTo(startTimeNew) >= 0 && startTime.compareTo(startTimeNew) <= 0){
return true;
}

boolean oldSpan = false;
boolean newSpan = false;

LocalDateTime startLocalDateTime = LocalDateTime.of(LocalDate.now(),startTime);
LocalDateTime endLocalDateTime;
if(startTime.compareTo(endTime) > 0){
oldSpan = true;
endLocalDateTime = LocalDateTime.of(LocalDate.now().plusDays(1),endTime);
}else{
endLocalDateTime = LocalDateTime.of(LocalDate.now(),endTime);
}
LocalDateTime startLocalDateTimeNew = LocalDateTime.of(LocalDate.now(),startTimeNew);
LocalDateTime endLocalDateTimeNew;
if(startTimeNew.compareTo(endTimeNew) > 0){
newSpan = true;
endLocalDateTimeNew = LocalDateTime.of(LocalDate.now().plusDays(1),endTimeNew);
}else{
endLocalDateTimeNew = LocalDateTime.of(LocalDate.now(),endTimeNew);
}

//并非两个都夸天
if(!(oldSpan && newSpan)){
if(oldSpan && startLocalDateTimeNew.getHour() < 12){
endLocalDateTimeNew = endLocalDateTimeNew.plusDays(1);
startLocalDateTimeNew = startLocalDateTimeNew.plusDays(1);
}
if(newSpan && startLocalDateTime.getHour() < 12){
endLocalDateTime = endLocalDateTime.plusDays(1);
startLocalDateTime = startLocalDateTime.plusDays(1);
}
}

if (startLocalDateTimeNew.compareTo(startLocalDateTime) >= 0 && startLocalDateTimeNew.compareTo(endLocalDateTime) <= 0) {
return true;
}
if (endLocalDateTimeNew.compareTo(startLocalDateTime) >= 0 && endLocalDateTimeNew.compareTo(endLocalDateTime) <= 0) {
return true;
}
if (startLocalDateTimeNew.compareTo(startLocalDateTime) <= 0 && endLocalDateTimeNew.compareTo(endLocalDateTime) >= 0) {
return true;
}

return false;
}
————————————————
版权声明:本文为CSDN博主「蔷薇思绪」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36270700/article/details/108777802

java计算两个时间段是否重合

原文:https://www.cnblogs.com/valky1374/p/14954780.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!