【发布时间】:2019-01-30 08:13:31
【问题描述】:
我需要从范围创建日期。
例子:
Start: 01.01.2017 16:30
End: 04.01.2017 23.30
预期结果:
01.01.2017 16:30
01.01.2017 23:00
01.02.2017 09:00
01.02.2017 23:00
01.03.2017 09:00
01.03.2017 23:00
01.04.2017 09:00
01.04.2017 23:00
01.04.2017 23.30
etc...
有没有更好的办法?
ZonedDateTime start = ZonedDateTime.now();
ZonedDateTime end = ZonedDateTime.now().plusDays(10);
List<ZonedDateTime> result = new ArrayList();
result.add(start);
while(start.isBefore(end) || start.compareTo(end)==0){
if(start.getHour == 23 || start.getMinute() == 0){
result.add(start);
}
if(start.getHour == 9 || start.getMinute() == 0){
result.add(start);
}
start = start.addMinutes(1);
}
result.add(end);
【问题讨论】:
-
什么是时间规则?
-
模式是什么?
-
只需添加正确的小时数 - 模式很明显。首先弄清楚如何和第一个间隔之间的区别。然后每次只需移动正确的小时数,直到从开始到 10 天。将比 10 天(10 * 365 * 60 次迭代)一分钟一分钟地迭代快很多。
-
不得不说您的描述与您的预期输出不符,对我来说,所以您只想在给定的天数内将
16:30和23.30添加到List?