首页 > 其他分享 >获取某段时间内的周一(二等等)的日期

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

时间:2022-11-24 12:44:23浏览次数:48  
标签:dates String 段时间 weekDays 获取 日期 ymd dateInterval

 /**
     * 获取某段时间内的周一(二等等)的日期
     *
     * @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;
    }

 

标签:dates,String,段时间,weekDays,获取,日期,ymd,dateInterval
From: https://www.cnblogs.com/tangbang/p/16921509.html

相关文章