1.不借助第三方插件库的方式
// 设置开始时间和结束时间
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(11, 0);
// 创建一个空的时间段列表
List<LocalTime> timeSlots = new ArrayList<>();
// 将时间段切割成15分钟一段,并将它们添加到列表中
LocalTime current = start;
while (current.isBefore(end)) {
timeSlots.add(current);
current = current.plusMinutes(15); //增加指定的分钟数
}
if (current.equals(end)) {
timeSlots.add(end);
}
// 打印时间段列表
for (LocalTime timeSlot : timeSlots) {
System.out.println(timeSlot);
}
---------------------------不借助第三方插件库的方式-----------------------------------
09:00
09:15
09:30
09:45
10:00
10:15
10:30
10:45
11:00
2.基于hutool的方式
List<DateTime> list = DateUtil.rangeToList(DateUtil.parse("2023-01-07 09:00"),
DateUtil.parse("2023-01-07 11:00"), DateField.MINUTE, 15);
for (DateTime time : list) {
System.out.println(time);
}
---------------------------基于hutool的方式-----------------------------------
2023-01-07 09:00:00
2023-01-07 09:15:00
2023-01-07 09:30:00
2023-01-07 09:45:00
2023-01-07 10:00:00
2023-01-07 10:15:00
2023-01-07 10:30:00
2023-01-07 10:45:00
2023-01-07 11:00:00