java根据时间间隔获取时间集合

    public static List<String> allRangeDate(String startDateStr, String endDateStr) {
        List<String> listDate = new ArrayList<>();
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            LocalDateTime startDate = LocalDateTime.parse(startDateStr, df1);
            LocalDateTime endDate = LocalDateTime.parse(endDateStr, df1);
            LocalDateTime tempDate = null;
            while (!(LocalDateTime.of(startDate.plusDays(-1).toLocalDate(), LocalTime.MIN)
                    .equals(LocalDateTime.of(endDate.toLocalDate(), LocalTime.MIN)))) {
                tempDate = startDate;
                String format = tempDate.format(df2);
                listDate.add(format);
                startDate = startDate.plusDays(1);
            }
            System.out.println(listDate.toString());
            return listDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

posted @ 2022-05-06 11:21  _Lawrence  阅读(651)  评论(0编辑  收藏  举报