获取某段时间的所有日期或者月份

 

 

 

 

 

 /**
     * 获取一段时间的每一天日期或者月份
     * 开始时间要小于结束时间
     * @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;
    }

 

posted @ 2021-06-24 12:07  未确定  阅读(182)  评论(0编辑  收藏  举报