工具類 - 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"]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM