package org.linlinjava.jz.db.util.hm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;

public class CalendarUtil {
	public static String DATE_FORMAT = "yyyy-MM-dd";
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM");
	public static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取上一月
	 *
	 * @return
	 */
	public static String getLastMonth(Date date) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		if (null != date) {
			cal.setTime(date);
		}
		cal.add(cal.MONTH, -1);
		String lastMonth = dft.format(cal.getTime());
		return lastMonth;
	}

	/**
	 * 下一月
	 *
	 * @return
	 */
	public static String getPreMonth(Date date) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		if (null != date) {
			cal.setTime(date);
		}
		cal.add(cal.MONTH, 1);
		String preMonth = dft.format(cal.getTime());
		return preMonth;
	}

	/**
	 * 当月
	 *
	 * @return
	 */
	public static String getCurrMonth(Date date) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		if (null != date) {
			cal.setTime(date);
		}
		String preMonth = dft.format(cal.getTime());
		return preMonth;
	}

	/**
	 * 当天
	 *
	 * @return
	 */
	public static String getCurrDay() {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		String preMonth = sdf.format(cal.getTime());
		return preMonth;
	}

	/**
	 * 获取某年某月的所有天 strDate 2020-02
	 *
	 * @return
	 */
	public static List<String> getDayListOfMonth(String strDate) {
		List<String> list = new ArrayList<String>();
		Calendar calendar = new GregorianCalendar();
		try {
			Date date1 = dft.parse(strDate);
			calendar.setTime(date1); // 放入你的日期
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

		for (int i = 1; i <= maxDay; i++) {
			String r = i + "";
			if (i < 10) {
				r = "0" + r;
			}
			String aDate = strDate + "-" + r;
			list.add(aDate);
		}

		return list;
	}

	/**
	 * 判断日期属于周几
	 *
	 * @param data
	 * @return
	 */
	public static int ofWeek(String data) {
		Calendar c = Calendar.getInstance();
		try {
			Date date = sdf.parse(data);
			c.setTime(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
//		System.out.println(dayForWeek);
		return dayForWeek;
	}

	/**
	 * 获取指定年月的第一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		// 设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH, firstDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定年月的第一天
	 * 
	 * @param str 2020-10
	 * @return
	 */
	public static String getFirstDayOfMonth(String str) {
		String[] split = str.split("-");
		if(split.length!=2) {
			return "";
		}
		String firstDayOfMonth = getFirstDayOfMonth(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
		return firstDayOfMonth;
	}

	/**
	 * 获取指定年月的最后一天
	 *
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定年月的最后一天
	 * 
	 * @param str 2020-10
	 * @return
	 */
	public static String getLastDayOfMonth(String str) {
		String[] split = str.split("-");
		if(split.length!=2) {
			return "";
		}
		String lastDayOfMonth = getLastDayOfMonth(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
		return lastDayOfMonth;
	}

	public static void main(String[] args) throws ParseException {
//        List<String> dayListOfMonth = getDayListOfMonth("2020-02");
//        System.out.println(dayListOfMonth);
		System.out.println(getFirstDayOfMonth(2021, 3));

	}
}
