请再发一遍
时间: 2024-02-11 19:09:18 浏览: 110
再一次发计算器
好的,以下是可能的 Java 代码实现:
```java
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String workTimeStr = "13:00-14:00;16:00-18:00";
String availableTimeStr = "13:58-14:00;16:00-16:02";
List<TimePeriod> workTime = parseTimeStr(workTimeStr);
List<TimePeriod> availableTime = parseTimeStr(availableTimeStr);
boolean hasAdjacentTime = false;
for (int i = 0; i < availableTime.size() - 1; i++) {
TimePeriod curPeriod = availableTime.get(i);
TimePeriod nextPeriod = availableTime.get(i + 1);
if (curPeriod.getEndTime().equals(nextPeriod.getStartTime())) {
for (int j = 1; j < workTime.size(); j++) {
TimePeriod preWorkPeriod = workTime.get(j - 1);
TimePeriod curWorkPeriod = workTime.get(j);
if (preWorkPeriod.getEndTime().equals(curPeriod.getEndTime())
&& curWorkPeriod.getStartTime().equals(nextPeriod.getStartTime())) {
hasAdjacentTime = true;
break;
}
}
}
}
if (hasAdjacentTime) {
System.out.println("找到相邻的时间段!");
} else {
System.out.println("没有相邻的时间段。");
}
}
private static List<TimePeriod> parseTimeStr(String timeStr) {
List<TimePeriod> timeList = new ArrayList<>();
String[] periodStrs = timeStr.split(";");
for (String periodStr : periodStrs) {
String[] timeStrs = periodStr.split("-");
LocalTime startTime = LocalTime.parse(timeStrs[0]);
LocalTime endTime = LocalTime.parse(timeStrs[1]);
timeList.add(new TimePeriod(startTime, endTime));
}
return timeList;
}
}
class TimePeriod {
private LocalTime startTime;
private LocalTime endTime;
public TimePeriod(LocalTime startTime, LocalTime endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
public LocalTime getStartTime() {
return startTime;
}
public LocalTime getEndTime() {
return endTime;
}
}
```
其中,`TimePeriod` 类表示一个时间段,包含开始时间和结束时间;`parseTimeStr` 方法用于将字符串形式的时间段转换为 `TimePeriod` 对象的集合;`main` 方法中的主要逻辑是遍历可用工作时间段,判断它们是否和工作时间段中的相邻两个时段重合。如果找到相邻的时间段,则输出提示信息。
阅读全文