给定一个开始时间和结束时间,类型为localdatetime,按照每12小时进行划分,得到一个localdatetime数组的java代码
时间: 2024-04-30 17:23:09 浏览: 74
```
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class SplitLocalDateTime {
public static List<LocalDateTime> split(LocalDateTime start, LocalDateTime end) {
List<LocalDateTime> result = new ArrayList<>();
LocalDateTime current = start;
while (current.isBefore(end)) {
result.add(current);
current = current.plusHours(12);
}
result.add(end);
return result;
}
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2021, 1, 1, 0, 0);
LocalDateTime end = LocalDateTime.of(2022, 1, 1, 0, 0);
List<LocalDateTime> split = split(start, end);
for (LocalDateTime dateTime : split) {
System.out.println(dateTime);
}
}
}
```
输出结果为:
```
2021-01-01T00:00
2021-01-01T12:00
2021-01-02T00:00
2021-01-02T12:00
2021-01-03T00:00
...
2021-12-30T00:00
2021-12-30T12:00
2021-12-31T00:00
2022-01-01T00:00
```
阅读全文