USVT-108 算法中的问题导致月份的查询不包含当前月份

This commit is contained in:
YuCheng Hu 2022-11-13 15:10:00 -05:00
parent 16f84faa5b
commit 35813c24b7
1 changed files with 11 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import lombok.Data;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
@ -71,14 +72,22 @@ public class DateRange {
}
/**
* Get Month Key, this key will include current month.
*
* @return
*/
public List<String> getMonthKeys() {
List<String> monthKeys = new ArrayList<>();
LocalDate currentDate = this.start;
while (currentDate.isBefore(this.end)) {
monthKeys.add(currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM")));
long monthsBetween = ChronoUnit.MONTHS.between(start, end);
for (int i = 0; i < monthsBetween; i++) {
currentDate = currentDate.plusMonths(1);
monthKeys.add(currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM")));
}
return monthKeys;