Java 获取一段时间内,每周几或者每月几号的详细日期,结果为日期数组

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Test {
    public static void main(String[] args) throws ParseException {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse("2022-11-15");//开始时间(包含该天)
        Date endDate = sdf.parse("2023-11-15");//结束时间(不包含该天)
        String daysOfWeekInput = "1,7";  //每周1,日
        String daysOfMonthInput = "4,13,31"; //每月4号,13号,31号


        List dateFromWeek = getDateFromWeek(sdf, startDate, endDate, daysOfWeekInput);
        System.out.println("dateFromWeek" + dateFromWeek);
        //[2022-11-20, 2022-11-21, 2022-11-27, 2022-11-28, 2022-12-04, 2022-12-05, 2022-12-11, 2022-12-12, 2022-12-18, 2022-12-19, 2022-12-25, 2022-12-26, 2023-01-01, 2023-01-02, 2023-01-08, 2023-01-09, 2023-01-15, 2023-01-16, 2023-01-22, 2023-01-23, 2023-01-29, 2023-01-30, 2023-02-05, 2023-02-06, 2023-02-12, 2023-02-13, 2023-02-19, 2023-02-20, 2023-02-26, 2023-02-27, 2023-03-05, 2023-03-06, 2023-03-12, 2023-03-13, 2023-03-19, 2023-03-20, 2023-03-26, 2023-03-27, 2023-04-02, 2023-04-03, 2023-04-09, 2023-04-10, 2023-04-16, 2023-04-17, 2023-04-23, 2023-04-24, 2023-04-30, 2023-05-01, 2023-05-07, 2023-05-08, 2023-05-14, 2023-05-15, 2023-05-21, 2023-05-22, 2023-05-28, 2023-05-29, 2023-06-04, 2023-06-05, 2023-06-11, 2023-06-12, 2023-06-18, 2023-06-19, 2023-06-25, 2023-06-26, 2023-07-02, 2023-07-03, 2023-07-09, 2023-07-10, 2023-07-16, 2023-07-17, 2023-07-23, 2023-07-24, 2023-07-30, 2023-07-31, 2023-08-06, 2023-08-07, 2023-08-13, 2023-08-14, 2023-08-20, 2023-08-21, 2023-08-27, 2023-08-28, 2023-09-03, 2023-09-04, 2023-09-10, 2023-09-11, 2023-09-17, 2023-09-18, 2023-09-24, 2023-09-25, 2023-10-01, 2023-10-02, 2023-10-08, 2023-10-09, 2023-10-15, 2023-10-16, 2023-10-22, 2023-10-23, 2023-10-29, 2023-10-30, 2023-11-05, 2023-11-06, 2023-11-12, 2023-11-13]

        List dateFromMonth = getDateFromMonth(sdf, startDate, endDate, daysOfMonthInput);
        System.out.println("dateFromMonth" + dateFromMonth);
        //[2022-12-04, 2022-12-13, 2022-12-31, 2023-01-04, 2023-01-13, 2023-01-31, 2023-02-04, 2023-02-13, 2023-03-04, 2023-03-13, 2023-03-31, 2023-04-04,2023-04-13, 2023-05-04, 2023-05-13, 2023-05-31, 2023-06-04, 2023-06-13, 2023-07-04, 2023-07-13, 2023-07-31, 2023-08-04, 2023-08-13, 2023-08-31, 2023-09-04, 2023-09-13, 2023-10-04, 2023-10-13, 2023-10-31, 2023-11-04, 2023-11-13]

        
    private static List getDateFromWeek(SimpleDateFormat sdf, Date startDate, Date endDate, String daysOfWeekInput) {
        List dayWeekResult = new ArrayList<>();
        while (startDate.before(endDate)) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (dayOfWeek == 0) {
                dayOfWeek = 7;
            }
            if (daysOfWeekInput.contains(dayOfWeek + "")) {
                dayWeekResult.add(sdf.format(startDate));
            }
            cal.add(Calendar.DATE, 1);
            startDate = cal.getTime();
        }
        return dayWeekResult;
    }

    private static List getDateFromMonth(SimpleDateFormat sdf, Date startDate, Date endDate, String daysOfMonthInput) {
        List daysOfMonthInputList = Arrays.asList(daysOfMonthInput.split(","));
        List dayMonthResult = new ArrayList<>();
        while (startDate.before(endDate)) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);
            int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
            if (daysOfMonthInputList.contains(dayOfMonth+"")) {
                dayMonthResult.add(sdf.format(startDate));
            }
            cal.add(Calendar.DATE, 1);
            startDate = cal.getTime();
        }
        return dayMonthResult;
    }
}

你可能感兴趣的:(java,开发语言)