以上代码优化下,需要传入4个参数,第一个时间段的开始时间,第一个时间段的结束时间,要检查的时间段的开始时间,要检查的时间段的结束时间,java实现,考虑跨夜
时间: 2023-09-12 16:12:56 浏览: 33
以下是一个优化后的实现,可以跨夜并且只需要传入一个时间段和要检查的时间段:
```java
import java.time.LocalTime;
public class TimeUtils {
public static boolean isTimeInRange(LocalTime start, LocalTime end, LocalTime checkStart, LocalTime checkEnd) {
// 处理跨夜情况
if (end.isBefore(start)) {
end = end.plusDays(1);
}
if (checkEnd.isBefore(checkStart)) {
checkEnd = checkEnd.plusDays(1);
}
// 判断是否在时间段内
return !checkEnd.isBefore(start) && !checkStart.isAfter(end);
}
}
```
这个方法与之前的方法不同,只需要传入一个时间段和要检查的时间段,并且将时间段的判断逻辑简化了。首先处理跨夜情况,将结束时间加上一天。然后,判断要检查的时间段是否在这个时间段内即可。如果要检查的时间段的结束时间在开始时间之前,或者开始时间在结束时间之后,那么就不在时间段内。最后,使用 ! 操作符将结果取反,返回是否在时间段内。
相关问题
以上代码优化下,需要传入6个参数,第一个时间段的开始时间,第一个时间段的结束时间,第二个时间段的开始时间,第二个时间段的结束时间,要检查的时间段的开始时间,要检查的时间段的结束时间,java实现,考虑跨夜
以下是一个优化后的实现,可以跨夜并且只需要传入两个时间段和要检查的时间段:
```java
import java.time.LocalTime;
public class TimeUtils {
public static boolean isTimeInRange(LocalTime start1, LocalTime end1, LocalTime start2, LocalTime end2, LocalTime checkStart, LocalTime checkEnd) {
// 转换为秒数
long start1Seconds = start1.toSecondOfDay();
long end1Seconds = end1.toSecondOfDay();
long start2Seconds = start2.toSecondOfDay();
long end2Seconds = end2.toSecondOfDay();
long checkStartSeconds = checkStart.toSecondOfDay();
long checkEndSeconds = checkEnd.toSecondOfDay();
// 处理跨夜情况
if (end1.isBefore(start1)) {
end1Seconds += 24 * 60 * 60;
}
if (end2.isBefore(start2)) {
end2Seconds += 24 * 60 * 60;
}
if (checkEnd.isBefore(checkStart)) {
checkEndSeconds += 24 * 60 * 60;
}
// 判断是否在时间段内
if (checkStartSeconds >= start1Seconds && checkEndSeconds <= end1Seconds) {
return true;
}
if (checkStartSeconds >= start2Seconds && checkEndSeconds <= end2Seconds) {
return true;
}
return false;
}
}
```
这个方法将每个时间段转换为秒数进行比较,处理跨夜情况时,将结束时间加上 24 小时的秒数。最后,判断要检查的时间段是否在两个时间段内即可。
以上代码,传入一个时间段比如01:00-02:00,考虑到跨夜
如果要考虑跨夜的情况,可以对代码进行适当修改。以下是一个修改后的示例代码:
```java
import java.time.LocalTime;
public class TimeRange {
public static void main(String[] args) {
LocalTime startTime = LocalTime.parse(args[0]);
LocalTime endTime = LocalTime.parse(args[1]);
LocalTime noon = LocalTime.of(12, 0);
LocalTime midnight = LocalTime.of(0, 0);
if (startTime.isBefore(noon) && endTime.isBefore(noon)) {
System.out.println("The time range is in the first half of the day (00:00-11:59).");
} else if (startTime.isAfter(noon) && endTime.isAfter(noon)) {
System.out.println("The time range is in the second half of the day (12:00-23:59).");
} else if (endTime.isBefore(startTime)) {
// 处理跨夜的情况
if (endTime.isAfter(midnight) && startTime.isBefore(noon)) {
System.out.println("The time range is in the first half of the day (00:00-11:59).");
} else {
System.out.println("The time range is in the second half of the day (12:00-23:59).");
}
} else {
System.out.println("The time range straddles both halves of the day.");
}
}
}
```
在修改后的代码中,我们定义了一个午夜的时间(00:00),并在判断跨夜的情况时使用了它。如果结束时间在开始时间之前,就说明时间段跨夜了。这时我们需要判断开始时间和结束时间所在的区间,以确定时间段属于哪个区间。
例如,如果传入的时间段是01:00-02:00,程序将输出:
```
The time range is in the first half of the day (00:00-11:59).
```
因为这个时间段跨夜,且结束时间在中午之前,所以它属于第一个区间(00:00-11:59)。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)