以上代码优化下,传入四个参数
时间: 2023-07-27 19:31:35 浏览: 43
如果要传入四个参数来表示开始时间和结束时间所在的日期和时间,可以修改代码如下:
```java
import java.time.LocalDateTime;
import java.time.LocalTime;
public class TimeRange {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(
Integer.parseInt(args[0]), // year
Integer.parseInt(args[1]), // month
Integer.parseInt(args[2]), // day
Integer.parseInt(args[3]), // hour
Integer.parseInt(args[4]), // minute
Integer.parseInt(args[5]) // second
);
LocalDateTime end = LocalDateTime.of(
Integer.parseInt(args[6]), // year
Integer.parseInt(args[7]), // month
Integer.parseInt(args[8]), // day
Integer.parseInt(args[9]), // hour
Integer.parseInt(args[10]), // minute
Integer.parseInt(args[11]) // second
);
LocalTime noon = LocalTime.of(12, 0);
LocalTime midnight = LocalTime.of(0, 0);
LocalTime startTime = start.toLocalTime();
LocalTime endTime = end.toLocalTime();
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.");
}
}
}
```
这个代码与之前的代码类似,只是在开始和结束时间的处理上有所变化。我们使用LocalDateTime类来表示完整的日期和时间信息,然后通过toLocalTime()方法将其转换为LocalTime对象。其余的代码逻辑与之前的代码相同。
例如,如果传入的四个参数是2022、10、1、23、30、0和2022、10、2、0、30、0,程序将输出:
```
The time range is in the second half of the day (12:00-23:59).
```
因为这个时间段跨夜,且开始时间在中午之后,所以它属于第二个区间(12:00-23:59)。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)