json字符换转对象,对象转字符串,返回指定日期是星期几,返回指定时间间隔内指定星期的日期列表

package demo15;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @author tuzuoquan
 * @date 2022/2/4 14:19
 */
public class Demo {

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

//        List<DateRange> list = new ArrayList<>();
//        DateRange dateRange = new DateRange();
//        dateRange.setBeginDate("2021-06-09");
//        dateRange.setEndDate("2021-06-18");
//        list.add(dateRange);
//
//        DateRange dateRange2 = new DateRange();
//        dateRange2.setBeginDate("2021-07-09");
//        dateRange2.setEndDate("2021-07-18");
//        list.add(dateRange2);
//
//        String jsonObject = JSONObject.toJSONString(list);
//        System.out.println(jsonObject);
//
//        JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(dateRange);
//        System.out.println(jsonObject1.get("beginDate"));
//
//        List<DateRange> list1 = JSON.parseArray(jsonObject, DateRange.class);
//        System.out.println(list1.toString());
//        list1.stream().forEach(item -> {
//            System.out.println(item.toString());
//        });
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date startDate = sdf.parse("2022-01-06");
//        Calendar startCalendar = Calendar.getInstance();
//        startCalendar.setTime(startDate);
//
//        Date endDate = sdf.parse("2022-02-06");
//        Calendar endCalendar = Calendar.getInstance();
//        endCalendar.setTime(endDate);
//
//        String startWeek = "1,2";
//        Calendar calendar = getRealStartDay(startWeek, startCalendar, endCalendar);
//
//        String week = dateToWeek("2022-02-09");
//        System.out.println(week);

//        List<String> days = getDayOfWeekWithInDateInterval("2022-01-06", "2022-02-06", 1);
//        days.stream().forEach(day -> {
//            System.out.println(day);
//        });

        String beginDate = "2022-01-06";
        String endDate = "2022-02-06";
        String startWeek = "1,2";

        String[] weekDays1 = startWeek.split("[^\\d]+");
        List<String> weekDayList = new ArrayList<>();
        for(int i = 0 ; i < weekDays1.length; i++) {
            List<String> weekListTmp = getDayOfWeekWithInDateInterval(beginDate, endDate, Integer.parseInt(weekDays1[i]));
            if (CollectionUtils.isEmpty(weekListTmp)) {
                continue;
            }
            weekDayList.addAll(weekListTmp);
        }
        weekDayList.stream().forEach(week -> {
            System.out.println(week);
        });
    }
    
    /**
     * 返回beginDate,endDate之间指定周weekDays的日期字符串。其中weekDays的内容如下:
     * 
     * 周日 SUNDAY = 1
     * 周一 MONDAY = 2
     * 周二 TUESDAY = 3
     * 周三 WEDNESDAY = 4
     * 周四 THURSDAY = 5
     * 周五 FRIDAY = 6
     * 周六 SATURDAY = 7
     **/
    public static List<String> getDayOfWeekWithInDateInterval(String beginDate, String endDate, int weekDays) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dateResult = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        String[] dateInterval = {beginDate, endDate};
        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();
        }
        for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
            cal.setTime(date);
            if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
                String format = sdf.format(date);
                dateResult.add(format);

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

    /**
     * 获取指定日期是星期几
     **/
    public static String dateToWeek(String datetime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = sdf.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDays[w];
    }
}

DateRange的内容如下:

import java.io.Serializable;

/**
 * @author tuzuoquan
 * @date 2022/2/4 14:19
 */
public class DateRange implements Serializable {

    private String beginDate;
    private String endDate;

    public String getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(String beginDate) {
        this.beginDate = beginDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String  toString() {
        return "DateRange{" +
                "beginDate='" + beginDate + '\'' +
                ", endDate='" + endDate + '\'' +
                '}';
    }
}
上一篇:java 封装


下一篇:将数组里某个属性相同的对象合并成一个数组