获取两个日期中每月指定日()

1:导入依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>

2:代码

/**
     * 获取两个日期中每月指定日
     *
     * @param startDate
     * @param endDate
     * @param appointDate 指定日  多个逗号隔开
     * @return
     */
    public static List<String> getBetweenMonthAppointDate(String startDate, String endDate, String appointDate) {
        //获取两个日期中的所有月
        List<String> list = getAllDateByParamDate(startDate, endDate, "month");
        // 转化成日期类型
        Date startDateD = DateUtils.strToDate(startDate, DateUtils.DATE_PATTERN_DEFAULT);
        Date endDateD = DateUtils.strToDate(endDate, DateUtils.DATE_PATTERN_DEFAULT);
        List<String> resultList = new ArrayList<>();
        if (StringUtils.isEmpty(appointDate)) {
            return resultList;
        }
        List<String> dayList = Arrays.asList(appointDate.split(","));
        for (String date : list) {
            for(String day:dayList){
                String dayNew = Integer.valueOf(day) > 9 ? day : "0" + day;
                String _date = date + "-"+dayNew;
                if (isValidDate(_date)) {
                    Date comStart = DateUtils.strToDate(_date, DateUtils.DATE_PATTERN_DEFAULT);
                    if (startDateD.getTime() <= comStart.getTime() && comStart.getTime() <= endDateD.getTime()) {
                        resultList.add(nextEffectiveDate(comStart));
                    }
                }
            }
        }
        return resultList;

    }

    public static List<String> getAllDateByParamDate(String startDate, String endDate, String type) {
        if (!"year".equals(type) && !"month".equals(type) && !"day".equals(type)) {
            return null;
        }
        DateTime parseStartDate = DateUtil.parse(startDate);
        DateTime parseEndDate = DateUtil.parse(endDate);
        // 存储所有日期的list
        ArrayList<String> list = new ArrayList<>();
        // 获取所有年份
        if (type.equals("year")) {
            list.add(parseStartDate.toString("yyyy"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.YEAR, -1);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.YEAR, 1);
                list.add(stageDateTime.toString("yyyy"));
                parseStartDate = stageDateTime;
            }
        } else if (type.equals("month")) {
            // 获取所有月份
            list.add(parseStartDate.toString("yyyy-MM"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.MONTH, 0);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM"));
                parseStartDate = stageDateTime;
            }
        } else {
            // 获取所有日期
            list.add(parseStartDate.toString("yyyy-MM-dd"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.DAY_OF_MONTH, -1);
            while (parseStartDate.isBeforeOrEquals(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.DAY_OF_MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM-dd"));
                parseStartDate = stageDateTime;
            }
        }
        // 返回数据
        return list;
    }

    /**
     * 根据指定日期获取下个月的这一天
     * 特殊例子:如果是月末31号或30号,而下个月没有这一天,将获取下月月末。
     * 如:2022/1/31获取下个月这一天结果为2022/2/28
     *
     * @param effectiveDate
     * @return
     * @throws ParseException
     */
    public static String nextEffectiveDate(Date effectiveDate) {
        int year = DateUtil.year(effectiveDate);
        int nextMonth = DateUtil.month(effectiveDate) + 1;
        int day = DateUtil.dayOfMonth(effectiveDate);

        if (nextMonth > 12) {
            nextMonth = 1;
            year += 1;
        }
        String nextEffectiveDay = appendDate(year, nextMonth, day);
        if (!isValidDate(nextEffectiveDay)) {
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(effectiveDate);
//            calendar.set(Calendar.YEAR, year);
//            calendar.set(Calendar.MONTH, nextMonth);
//            calendar.set(Calendar.DAY_OF_MONTH, 0);
//            nextEffectiveDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            nextEffectiveDay = "";
        }
        return nextEffectiveDay;
    }

    /**
     * 日期拼接
     */
    public static String appendDate(int year, int month, int day) {
        StringBuilder dateStr = new StringBuilder();
        dateStr.append(year)
                .append('-')
                .append(month > 9 ? month : "0" + month)
                .append('-')
                .append(day > 9 ? day : "0" + day);
        return dateStr.toString();
    }

    /**
     * 判断一个日期是否合法
     */
    public static boolean isValidDate(String dateStr) {
        Boolean convertStatus = true;
        //指定日期格式为四位年、两位月份、两位日期,注意 yyyy-MM-dd 区分大小写
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //设置lenient为false,否则SimpleDateFormat会比较宽松地验证日期。比如:2020-02-29会被接受,并转换成2020-03-01
        sdf.setLenient(false);
        try {
            sdf.parse(dateStr);
        } catch (ParseException e) {
            //日期格式不正确
            convertStatus = false;
        }
        return convertStatus;
    }
————————

1:导入依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>

2:代码

/**
     * 获取两个日期中每月指定日
     *
     * @param startDate
     * @param endDate
     * @param appointDate 指定日  多个逗号隔开
     * @return
     */
    public static List<String> getBetweenMonthAppointDate(String startDate, String endDate, String appointDate) {
        //获取两个日期中的所有月
        List<String> list = getAllDateByParamDate(startDate, endDate, "month");
        // 转化成日期类型
        Date startDateD = DateUtils.strToDate(startDate, DateUtils.DATE_PATTERN_DEFAULT);
        Date endDateD = DateUtils.strToDate(endDate, DateUtils.DATE_PATTERN_DEFAULT);
        List<String> resultList = new ArrayList<>();
        if (StringUtils.isEmpty(appointDate)) {
            return resultList;
        }
        List<String> dayList = Arrays.asList(appointDate.split(","));
        for (String date : list) {
            for(String day:dayList){
                String dayNew = Integer.valueOf(day) > 9 ? day : "0" + day;
                String _date = date + "-"+dayNew;
                if (isValidDate(_date)) {
                    Date comStart = DateUtils.strToDate(_date, DateUtils.DATE_PATTERN_DEFAULT);
                    if (startDateD.getTime() <= comStart.getTime() && comStart.getTime() <= endDateD.getTime()) {
                        resultList.add(nextEffectiveDate(comStart));
                    }
                }
            }
        }
        return resultList;

    }

    public static List<String> getAllDateByParamDate(String startDate, String endDate, String type) {
        if (!"year".equals(type) && !"month".equals(type) && !"day".equals(type)) {
            return null;
        }
        DateTime parseStartDate = DateUtil.parse(startDate);
        DateTime parseEndDate = DateUtil.parse(endDate);
        // 存储所有日期的list
        ArrayList<String> list = new ArrayList<>();
        // 获取所有年份
        if (type.equals("year")) {
            list.add(parseStartDate.toString("yyyy"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.YEAR, -1);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.YEAR, 1);
                list.add(stageDateTime.toString("yyyy"));
                parseStartDate = stageDateTime;
            }
        } else if (type.equals("month")) {
            // 获取所有月份
            list.add(parseStartDate.toString("yyyy-MM"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.MONTH, 0);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM"));
                parseStartDate = stageDateTime;
            }
        } else {
            // 获取所有日期
            list.add(parseStartDate.toString("yyyy-MM-dd"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.DAY_OF_MONTH, -1);
            while (parseStartDate.isBeforeOrEquals(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.DAY_OF_MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM-dd"));
                parseStartDate = stageDateTime;
            }
        }
        // 返回数据
        return list;
    }

    /**
     * 根据指定日期获取下个月的这一天
     * 特殊例子:如果是月末31号或30号,而下个月没有这一天,将获取下月月末。
     * 如:2022/1/31获取下个月这一天结果为2022/2/28
     *
     * @param effectiveDate
     * @return
     * @throws ParseException
     */
    public static String nextEffectiveDate(Date effectiveDate) {
        int year = DateUtil.year(effectiveDate);
        int nextMonth = DateUtil.month(effectiveDate) + 1;
        int day = DateUtil.dayOfMonth(effectiveDate);

        if (nextMonth > 12) {
            nextMonth = 1;
            year += 1;
        }
        String nextEffectiveDay = appendDate(year, nextMonth, day);
        if (!isValidDate(nextEffectiveDay)) {
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(effectiveDate);
//            calendar.set(Calendar.YEAR, year);
//            calendar.set(Calendar.MONTH, nextMonth);
//            calendar.set(Calendar.DAY_OF_MONTH, 0);
//            nextEffectiveDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
            nextEffectiveDay = "";
        }
        return nextEffectiveDay;
    }

    /**
     * 日期拼接
     */
    public static String appendDate(int year, int month, int day) {
        StringBuilder dateStr = new StringBuilder();
        dateStr.append(year)
                .append('-')
                .append(month > 9 ? month : "0" + month)
                .append('-')
                .append(day > 9 ? day : "0" + day);
        return dateStr.toString();
    }

    /**
     * 判断一个日期是否合法
     */
    public static boolean isValidDate(String dateStr) {
        Boolean convertStatus = true;
        //指定日期格式为四位年、两位月份、两位日期,注意 yyyy-MM-dd 区分大小写
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //设置lenient为false,否则SimpleDateFormat会比较宽松地验证日期。比如:2020-02-29会被接受,并转换成2020-03-01
        sdf.setLenient(false);
        try {
            sdf.parse(dateStr);
        } catch (ParseException e) {
            //日期格式不正确
            convertStatus = false;
        }
        return convertStatus;
    }