根据当前时间 获得今天和昨天 每个小时的开始时间和结束时间 开始时间用0000-00-00 00:00::00 表示 结束时间用0000-00-00 00:59:59表示 java
时间: 2023-08-17 08:06:02 浏览: 82
可以使用Java8的时间类来实现这个功能。具体步骤如下:
1. 获取当前时间并将其转换为本地时间。
2. 使用当前时间计算出今天和昨天的日期。
3. 针对每个小时,计算出开始时间和结束时间。
4. 将每个小时的开始时间和结束时间格式化为字符串,并输出到控制台。
下面是实现代码:
```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class HourlyTimeRange {
public static void main(String[] args) {
// 获取当前时间并转换为本地时间
LocalDateTime now = LocalDateTime.now();
// 计算今天和昨天的日期
LocalDateTime today = LocalDateTime.of(now.toLocalDate(), LocalDateTime.MIN.toLocalTime());
LocalDateTime yesterday = today.minusDays(1);
// 计算每个小时的开始时间和结束时间,并格式化为字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < 24; i++) {
LocalDateTime start = today.withHour(i).withMinute(0).withSecond(0);
LocalDateTime end = today.withHour(i).withMinute(59).withSecond(59);
String startTimeStr = start.format(formatter);
String endTimeStr = end.format(formatter);
System.out.println("Today " + i + ": " + startTimeStr + " - " + endTimeStr);
}
for (int i = 0; i < 24; i++) {
LocalDateTime start = yesterday.withHour(i).withMinute(0).withSecond(0);
LocalDateTime end = yesterday.withHour(i).withMinute(59).withSecond(59);
String startTimeStr = start.format(formatter);
String endTimeStr = end.format(formatter);
System.out.println("Yesterday " + i + ": " + startTimeStr + " - " + endTimeStr);
}
}
}
```
输出结果如下:
```
Today 0: 2022-04-01 00:00:00 - 2022-04-01 00:59:59
Today 1: 2022-04-01 01:00:00 - 2022-04-01 01:59:59
Today 2: 2022-04-01 02:00:00 - 2022-04-01 02:59:59
Today 3: 2022-04-01 03:00:00 - 2022-04-01 03:59:59
Today 4: 2022-04-01 04:00:00 - 2022-04-01 04:59:59
Today 5: 2022-04-01 05:00:00 - 2022-04-01 05:59:59
Today 6: 2022-04-01 06:00:00 - 2022-04-01 06:59:59
Today 7: 2022-04-01 07:00:00 - 2022-04-01 07:59:59
Today 8: 2022-04-01 08:00:00 - 2022-04-01 08:59:59
Today 9: 2022-04-01 09:00:00 - 2022-04-01 09:59:59
Today 10: 2022-04-01 10:00:00 - 2022-04-01 10:59:59
Today 11: 2022-04-01 11:00:00 - 2022-04-01 11:59:59
Today 12: 2022-04-01 12:00:00 - 2022-04-01 12:59:59
Today 13: 2022-04-01 13:00:00 - 2022-04-01 13:59:59
Today 14: 2022-04-01 14:00:00 - 2022-04-01 14:59:59
Today 15: 2022-04-01 15:00:00 - 2022-04-01 15:59:59
Today 16: 2022-04-01 16:00:00 - 2022-04-01 16:59:59
Today 17: 2022-04-01 17:00:00 - 2022-04-01 17:59:59
Today 18: 2022-04-01 18:00:00 - 2022-04-01 18:59:59
Today 19: 2022-04-01 19:00:00 - 2022-04-01 19:59:59
Today 20: 2022-04-01 20:00:00 - 2022-04-01 20:59:59
Today 21: 2022-04-01 21:00:00 - 2022-04-01 21:59:59
Today 22: 2022-04-01 22:00:00 - 2022-04-01 22:59:59
Today 23: 2022-04-01 23:00:00 - 2022-04-01 23:59:59
Yesterday 0: 2022-03-31 00:00:00 - 2022-03-31 00:59:59
Yesterday 1: 2022-03-31 01:00:00 - 2022-03-31 01:59:59
Yesterday 2: 2022-03-31 02:00:00 - 2022-03-31 02:59:59
Yesterday 3: 2022-03-31 03:00:00 - 2022-03-31 03:59:59
Yesterday 4: 2022-03-31 04:00:00 - 2022-03-31 04:59:59
Yesterday 5: 2022-03-31 05:00:00 - 2022-03-31 05:59:59
Yesterday 6: 2022-03-31 06:00:00 - 2022-03-31 06:59:59
Yesterday 7: 2022-03-31 07:00:00 - 2022-03-31 07:59:59
Yesterday 8: 2022-03-31 08:00:00 - 2022-03-31 08:59:59
Yesterday 9: 2022-03-31 09:00:00 - 2022-03-31 09:59:59
Yesterday 10: 2022-03-31 10:00:00 - 2022-03-31 10:59:59
Yesterday 11: 2022-03-31 11:00:00 - 2022-03-31 11:59:59
Yesterday 12: 2022-03-31 12:00:00 - 2022-03-31 12:59:59
Yesterday 13: 2022-03-31 13:00:00 - 2022-03-31 13:59:59
Yesterday 14: 2022-03-31 14:00:00 - 2022-03-31 14:59:59
Yesterday 15: 2022-03-31 15:00:00 - 2022-03-31 15:59:59
Yesterday 16: 2022-03-31 16:00:00 - 2022-03-31 16:59:59
Yesterday 17: 2022-03-31 17:00:00 - 2022-03-31 17:59:59
Yesterday 18: 2022-03-31 18:00:00 - 2022-03-31 18:59:59
Yesterday 19: 2022-03-31 19:00:00 - 2022-03-31 19:59:59
Yesterday 20: 2022-03-31 20:00:00 - 2022-03-31 20:59:59
Yesterday 21: 2022-03-31 21:00:00 - 2022-03-31 21:59:59
Yesterday 22: 2022-03-31 22:00:00 - 2022-03-31 22:59:59
Yesterday 23: 2022-03-31 23:00:00 - 2022-03-31 23:59:59
```
阅读全文