package com.ym.mec.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2019/9/23
 */
public class DateUtils {

    private static final String CHINESE_DATA_FORMAT = "yyyy年MM月dd日 HH:mm:ss";

    /**
     * @Author: Joburgess
     * @Date: 2019/9/23
     * 获取时间段内对应星期数的集合
     */
    public static List<Date> getWeekDays(Date startTime, Date endTime, int weekNum){
        Calendar calendar = Calendar.getInstance();
        List<Date> dates=new ArrayList<>();
        for(Date x = startTime; x.compareTo(endTime) <= 0;){
            calendar.setTime(x);
            calendar.add(Calendar.DATE, 1);
            x = calendar.getTime();
            if(calendar.get(Calendar.DAY_OF_WEEK) == weekNum){
                dates.add(x);
            }
        }
        return dates;
    }

    /**
     * 时间转换为中文格式
     * @param date
     * @return
     */
    public static String date2ChineseDate(Date date){
        return new SimpleDateFormat(CHINESE_DATA_FORMAT).format(date);
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        List<Date> mondayCount = getWeekDays(simpleDateFormat.parse("2019-9-23"), simpleDateFormat.parse("2019-10-23"), Calendar.SUNDAY);
        System.out.println(mondayCount.size());
    }

}
