获取某段时间内的周一(二等等)的日期

 /**
     * 获取某段时间内的周一(二等等)的日期
     *
     * @param dataBegin 开始日期
     * @param dataEnd   结束日期
     * @param weekDays  获取周几,1-6代表周一到周六。0代表周日
     * @return 返回日期List
     */
    public static List<String> getDayOfWeekWithinDateInterval(String dataBegin, String dataEnd, String weekDays) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateResult = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        String[] dateInterval = {dataBegin, dataEnd};
        Date[] dates = new Date[dateInterval.length];
        for (int i = 0; i < dateInterval.length; i++) {
            String[] ymd = dateInterval[i].split("[^\\d]+");
            cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
            dates[i] = cal.getTime();
        }

        String[] weekDaysStr = weekDays.split(",");

        for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
            cal.setTime(date);
            for (int i = 0; i < weekDaysStr.length; i++) {
                if (cal.get(Calendar.DAY_OF_WEEK) - 1 == Integer.parseInt(weekDaysStr[i])) {
                    String format = sdf.format(date);
                    dateResult.add(format);
                }
            }

            cal.add(Calendar.DATE, 1);
            date = cal.getTime();
        }
        return dateResult;
    }

 

posted @ 2022-11-24 12:36  棒棒博客  阅读(32)  评论(0编辑  收藏  举报