获取两个日期之间的所有周开始日期
Get all start-of-the-week dates between two dates
我想显示两个日期之间所有周的开始日期。
假设,我选择的开始日期为 8th Dec 2015 - 30th Dec 2015
,那么它应该 return 结果:
6th Dec 2015
13th Dec 2015
20th Dec 2015
27th Dec 2015
如果您使用 JodaTime library (or willing to switch to it), which is a personal preference of mine, you can use their dayOfWeek()
function to do this. It returns a LocalDate.Property
object which you can then manipulate to get the minimum value(实际上是一周的开始)。
要获取您想要的日期和return该周的最短日期,请尝试以下操作:
LocalDate myDate = getSelectedDate();
return myDate.dayOfWeek().withMinimumValue();
要获取结束日期之前的所有日期,您可以循环:
List<LocalDate> weekDates = new ArrayList<>();
LocalDate tmp = getFirstDate().dayOfWeek().withMinimumValue();
// Loop until we surpass end date
while(tmp.isBefore(getEndDate())) {
weekDates.add(tmp);
tmp = tmp.plusWeeks(1);
}
我想显示两个日期之间所有周的开始日期。
假设,我选择的开始日期为 8th Dec 2015 - 30th Dec 2015
,那么它应该 return 结果:
6th Dec 2015
13th Dec 2015
20th Dec 2015
27th Dec 2015
如果您使用 JodaTime library (or willing to switch to it), which is a personal preference of mine, you can use their dayOfWeek()
function to do this. It returns a LocalDate.Property
object which you can then manipulate to get the minimum value(实际上是一周的开始)。
要获取您想要的日期和return该周的最短日期,请尝试以下操作:
LocalDate myDate = getSelectedDate();
return myDate.dayOfWeek().withMinimumValue();
要获取结束日期之前的所有日期,您可以循环:
List<LocalDate> weekDates = new ArrayList<>();
LocalDate tmp = getFirstDate().dayOfWeek().withMinimumValue();
// Loop until we surpass end date
while(tmp.isBefore(getEndDate())) {
weekDates.add(tmp);
tmp = tmp.plusWeeks(1);
}