一、了解两个时间比大小
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeCompare{
public static void main(String[] args) {
String time1 = "2022-01-01 12:00:00";
String time2 = "20212-01-01 13:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = sdf.parse(time1);
Date date2 = sdf.parse(time2);
if (date1.before(date2)) {
System.out.println(time1 + " 在 " + time2 + " 之前");
} else if (date1.after(date2)) {
System.out.println(time1 + " 在 " + time2 + " 之后");
} else {
System.out.println(time1 + " 和 " + time2 + " 相等");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
二、自己的需求
public class TimeOverlap {
//时间一和时间二是否重叠
public static boolean isOverLap(String start1, String end1, String start2, String end2) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1Start = sdf.parse(start1);
Date dateTime1End = sdf.parse(end1);
Date dateTime2Start = sdf.parse(start2);
Date dateTime2End = sdf.parse(end2);
// 检查时间段1的开始时间是否在时间段2的结束时间之后,或者时间段1的结束时间是否在时间段2的开始时间之前
return !(dateTime1End.before(dateTime2Start) || dateTime1Start.after(dateTime2End));
} catch (ParseException e) {
// throw new RuntimeException(e);
// 处理日期时间解析异常
System.err.println("无法解析日期时间: " + e.getMessage());
return false;
}
}
//时间一包含时间二
public static boolean isContainLap(String start1, String end1, String start2, String end2) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1Start = sdf.parse(start1);
Date dateTime1End = sdf.parse(end1);
Date dateTime2Start = sdf.parse(start2);
Date dateTime2End = sdf.parse(end2);
return !(dateTime1Start.before(dateTime2Start) && dateTime1End.after(dateTime2End));
} catch (ParseException e) {
// 处理日期时间解析异常
System.err.println("无法解析日期时间: " + e.getMessage());
return false;
}
}
//时间二包含时间一
public static boolean isContainNextLap(String start1, String end1, String start2, String end2) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1Start = sdf.parse(start1);
Date dateTime1End = sdf.parse(end1);
Date dateTime2Start = sdf.parse(start2);
Date dateTime2End = sdf.parse(end2);
return !(dateTime2Start.before(dateTime1Start) && dateTime2End.after(dateTime1End));
} catch (ParseException e) {
// 处理日期时间解析异常
System.err.println("无法解析日期时间: " + e.getMessage());
return false;
}
}
//判断时间一开始时间是否大于时间二开始时间
public static boolean isContainLtLap(String start1, String end1, String start2, String end2) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime1Start = sdf.parse(start1);
Date dateTime1End = sdf.parse(end1);
Date dateTime2Start = sdf.parse(start2);
Date dateTime2End = sdf.parse(end2);
return !(dateTime1Start.before(dateTime2Start) && dateTime1End.before(dateTime2End));
} catch (ParseException e) {
// 处理日期时间解析异常
System.err.println("无法解析日期时间: " + e.getMessage());
return false;
}
}
public static void main(String[] args) {
String start1 = "2023-08-24 09:00:00";
String end1 = "2023-08-24 10:00:00";
String start2 = "2023-08-24 09:30:00";
String end2 = "2023-08-24 11:00:00";
System.out.println(isOverlap(start1, end1, start2, end2)); // 输出:true
}
}
三、进行时间比对
//如果返回true,则说明时间有交集
if (isOverLap(start1, end1, start2, end2)){
//判断时间1是否包含时间二
if(isContainLap(start1, end1, start2, end2)){
//
}else if(isContainNextLap(start1, end1, start2, end2)){
//判断时间二是否包含时间一
}else if(isContainLtLap(start1, end1, start2, end2)){
//判断时间一开始时间是否大于时间二开始时间
}else{
}
}