獲取某段時間的所有日期或者月份
阿新 • • 發佈:2021-06-24
/** * 獲取一段時間的每一天日期或者月份 * 開始時間要小於結束時間 * @param start * @param end * @return * @throws Exception */ public static List<String> getBetweenDate(String start, String end, Integer type) { List<String> list = new ArrayList<>(); if(start.length()<8){ start+="-01"; end+="-01"; } LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = 0L; if (type == 1) { distance = ChronoUnit.DAYS.between(startDate, endDate); Stream.iterate(startDate, d-> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString())); } else if(type==2) { distance = ChronoUnit.MONTHS.between(startDate, endDate); Stream.iterate(startDate, d -> d.plusMonths(1)).limit(distance + 1).forEach(f -> list.add(f.toString().substring(0,7))); }return list; }