package org.springblade.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;

/**
 * Created By lidexi in 2021/6/29
 **/
public class DateUtil {

	public static List<String> getDateListBetween2Day(String stime, String etime, String date_pattern) throws ParseException {
		List<String> list = new ArrayList<String>();
		SimpleDateFormat df = new SimpleDateFormat(date_pattern);
		Calendar cal = Calendar.getInstance();
		Date sdate = df.parse(stime);
		Date edate = df.parse(etime);
		cal.setTime(sdate);
		while (edate.after(sdate)) {
			list.add(df.format(sdate));
			cal.add(Calendar.DAY_OF_MONTH, 1);
			sdate = cal.getTime();
		}
		list.add(df.format(edate));
		return list;
	}

	public static List<String> getMonthListBetween2Day(String stime, String etime, String date_pattern) throws ParseException {
		List<String> list = new ArrayList<String>();
		SimpleDateFormat df = new SimpleDateFormat(date_pattern);
		Calendar cal = Calendar.getInstance();
		Date sdate = df.parse(stime);
		Date edate = df.parse(etime);
		cal.setTime(sdate);
		while (edate.after(sdate)) {
			list.add(df.format(sdate));
			cal.add(Calendar.MONTH, 1);
			sdate = cal.getTime();
		}
		list.add(df.format(edate));
		return list;
	}

	public static void main(String[] args) throws ParseException {
		String beginDate = "2021-05-25";
		String endDate = "2021-05-31";
		List<String> dateListBetween2Day = getDateListBetween2Day(beginDate, endDate, "yyyy-MM-dd");
		System.out.println(dateListBetween2Day.toString());

		String beginMonth = "2021-05";
		String endMonth = "2022-08";
		List<String> monthListBetween2Day = getMonthListBetween2Day(beginMonth, endMonth, "yyyy-MM");
		System.out.println(monthListBetween2Day.toString());
	}
}
