Tools - java8 get all dates between two dates

public static List<String> getBetweenDate(String start, String end) {
List<String> list = new ArrayList<>();
LocalDate startDate = null;
LocalDate endDate = null;
try {
startDate = LocalDate.parse(start);
endDate = LocalDate.parse(end);
} catch (Exception e) {
throw new ServiceException("日期格式不正确。(日期示例:2019-12-26)");
}

if (ObjectUtils.equals(start, end)) {
list.add(start);
return list;
}

long distance = ChronoUnit.DAYS.between(startDate, endDate);
if (distance < 1) {
return list;
}
Stream.iterate(startDate, d -> {
return d.plusDays(1);
}).limit(distance + 1).forEach(f -> {
list.add(f.toString());
});
return list;
}

注:传参(2019-12-31,2019-12-31) 返回 ["2019-12-31"]

Guess you like

Origin www.cnblogs.com/light-train-union/p/12125705.html