工具类 - java8 获取两个日期间的所有日期

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"]
posted @ 2019-12-31 17:50  conn2019  阅读(2830)  评论(0编辑  收藏  举报