Java – get all week start dates between two dates

I want to show all week start dates between two dates

Assuming that I have selected the start date as December 8, 2015 to December 30, 2015, it should return the result:

6th Dec 2015
13th Dec 2015
20th Dec 2015
27th Dec 2015

resolvent:

If you are using the jodatime Library (or are willing to switch to it), which is my personal preference, you can use its dayofweek() function to perform this operation. It returns a localdate.property object, which you can operate on to obtain the minimum value (actually the beginning of the week)

To get the required date and return the minimum date of the week, try the following:

LocalDate myDate = getSelectedDate();
return myDate.dayOfWeek().withMinimumValue();

To get all dates before the due date, you can cycle through:

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);
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>