package com.zcloud.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 说明：日期处理(临时处理时间) 作者：zhangyue 官网：www.qdkjchina.com
 */
public class DateSysUtil {

	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
	private final static SimpleDateFormat sdfMonth = new SimpleDateFormat("MM");
	private final static SimpleDateFormat sdfOnlyDay = new SimpleDateFormat("dd");
	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
	private final static SimpleDateFormat sdfMDay = new SimpleDateFormat("yyyy-M-d");
	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");

	/**
	 * 获取YYYY格式
	 *
	 * @return
	 */
	public static String getSdfTimes() {
		return sdfTimes.format(new Date());
	}

	/**
	 * 获取YYYY格式
	 *
	 * @return
	 */
	public static String getYear(int i) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -i);
		return sdfYear.format(c.getTime());
	}

	public static String getMonth(int i) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -i);
		return sdfMonth.format(c.getTime());
	}

	/**
	 * 获取DD格式
	 *
	 * @return
	 */
	public static String getOnlyDay(int i) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -i);
		return sdfOnlyDay.format(c.getTime());
	}

	/**
	 * 获取YYYY-MM-DD格式
	 *
	 * @return
	 */
	public static String getDay(int i) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, -i);
		return sdfDay.format(c.getTime());
	}

	public static String MDay() {
		return sdfMDay.format(new Date());
	}

	/**
	 * 获取YYYYMMDD格式
	 *
	 * @return
	 */
	public static String getDays() {
		return sdfDays.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 *
	 * @return
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}

	/**
	 * @Title: compareDate
	 * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws @author fh
	 */
	public static boolean compareDate(String s, String e) {
		if (fomatDate(s) == null || fomatDate(e) == null) {
			return false;
		}
		return fomatDate(s).getTime() >= fomatDate(e).getTime();
	}

	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 格式化日期
	 *
	 * @return
	 */
	public static Date fomatDateTime(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 校验日期是否合法
	 *
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			return false; // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
		}
	}

	/**
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static int getDiffYear(String startTime, String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24))
					/ 365);
			return years;
		} catch (Exception e) {
			return 0; // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
		}
	}

	/**
	 * <li>功能描述：时间相减得到天数
	 *
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = null;
		Date endDate = null;
		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		// System.out.println("相隔的天数="+day);
		return day;
	}

	/**
	 * 得到n天之后的日期
	 *
	 * @param days
	 * @return
	 */
	public static String getAfterDayDate(String days) {
		int daysInt = Integer.parseInt(days);
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdfd.format(date);
		return dateStr;
	}

	/**
	 * 得到n天之后是周几
	 *
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);
		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，日期转字符串
	 *
	 * @param date
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String date2Str(Date date) {
		return date2Str(date, "yyyy-MM-dd HH:mm:ss");
	}

	public static String date3Str(Date date) {
		return date2Str(date, "yyyy-M-d");
	}

	/**
	 * 按照yyyy-MM-dd HH:mm:ss的格式，字符串转日期
	 *
	 * @param date
	 * @return
	 */
	public static Date str2Date(String date) {
		if (Tools.notEmpty(date)) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return new Date();
		} else {
			return null;
		}
	}

	/**
	 * 把时间根据时、分、秒转换为时间段
	 *
	 * @param StrDate
	 */
	public static String getTimes(String StrDate) {
		String resultTimes = "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date now;
		try {
			now = new Date();
			Date date = df.parse(StrDate);
			long times = now.getTime() - date.getTime();
			long day = times / (24 * 60 * 60 * 1000);
			long hour = (times / (60 * 60 * 1000) - day * 24);
			long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

			StringBuffer sb = new StringBuffer();
			// sb.append("发表于：");
			if (hour > 0) {
				sb.append(hour + "小时前");
			} else if (min > 0) {
				sb.append(min + "分钟前");
			} else {
				sb.append(sec + "秒前");
			}
			resultTimes = sb.toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultTimes;
	}

	/**
	 * 按照参数format的格式，日期转字符串
	 *
	 * @param date
	 * @param format
	 * @return
	 */
	public static String date2Str(Date date, String format) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.format(date);
		} else {
			return "";
		}
	}

	/**
	 * 获取当年的第一天
	 *
	 * @param year
	 * @author zhangyue
	 * @return
	 */
	public static String getCurrYearFirst(int i) {
		final Calendar cal = Calendar.getInstance();

		cal.add(Calendar.DAY_OF_MONTH, -i);

		final int last = cal.getActualMinimum(Calendar.DAY_OF_YEAR);

		cal.set(Calendar.DAY_OF_YEAR, last);

		return sdfDay.format(cal.getTime());
	}

	/**
	 * 获取当年的最后一天
	 *
	 * @param year
	 * @author zhangyue
	 * @return
	 */
	public static String getCurrYearLast(int i) {
		final Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -i);

		final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);

		cal.set(Calendar.DAY_OF_YEAR, last);

		return sdfDay.format(cal.getTime());
	}

	/**
	 * 获取当前季度第一天
	 *
	 * @author zhangyue
	 * @return
	 */
	public static String quarterStart(int i) {
		Date dBegin = new Date();
		Calendar calBegin = Calendar.getInstance();

		calBegin.add(Calendar.DAY_OF_MONTH, -i);
		// calBegin.setTime(dBegin);
		int remainder = calBegin.get(Calendar.MONTH) % 3;
		int month = remainder != 0 ? calBegin.get(Calendar.MONTH) - remainder : calBegin.get(Calendar.MONTH);

		calBegin.set(Calendar.MONTH, month);
		calBegin.set(Calendar.DAY_OF_MONTH, calBegin.getActualMinimum(Calendar.DAY_OF_MONTH));

		calBegin.setTime(calBegin.getTime());
		return sdfDay.format(calBegin.getTime());
	}

	/**
	 * 获取当前季度最后一天
	 *
	 * @author zhangyue
	 * @return
	 */
	public static String quarterEnd(int i) {
		Date dEnd = new Date();
		Calendar calendar = Calendar.getInstance();

		calendar.add(Calendar.DAY_OF_MONTH, -i);
		// calendar.setTime(dEnd);
		int remainder = (calendar.get(Calendar.MONTH) + 1) % 3;
		int month = remainder != 0 ? calendar.get(Calendar.MONTH) + (3 - remainder) : calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.setTime(calendar.getTime());
		return sdfDay.format(calendar.getTime());
	}

	/**
	 * 获取本月第一天日期
	 *
	 * @return
	 */
	public static String getMonthFirstDay(int i) {
		/*
		 * String date_str = "2019-02-15";
		 */

		Calendar cale = Calendar.getInstance();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		// cale.setTime(formatter.parse(date_str));
		Calendar c = Calendar.getInstance();
		cale.add(Calendar.DAY_OF_MONTH, -i);

		cale.add(Calendar.MONTH, 0);

		cale.set(Calendar.DAY_OF_MONTH, 1);

		String firstDayOfMonth = formatter.format(cale.getTime()); // 当月第一天 2019-02-01

		/*
		 * cale.add(Calendar.MONTH, 1);
		 *
		 * cale.set(Calendar.DAY_OF_MONTH, 0);
		 *
		 * String lastDayOfMonth = formatter.format(cale.getTime()); // 当月最后一天
		 * 2019-02-28
		 */ return sdfDay.format(cale.getTime());
	}

	/**
	 * 获取本月最后一天日期
	 *
	 * @return
	 */
	public static String getMonthEndDay(int i) {
		/*
		 * String date_str = "2019-02-15";
		 */

		Calendar cale = Calendar.getInstance();

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

		// cale.setTime(formatter.parse(date_str));
		Calendar c = Calendar.getInstance();
		cale.add(Calendar.DAY_OF_MONTH, -i);

		/*
		 * cale.add(Calendar.MONTH, 0);
		 *
		 * cale.set(Calendar.DAY_OF_MONTH, 1);
		 *
		 * String firstDayOfMonth = formatter.format(cale.getTime()); // 当月第一天
		 * 2019-02-01
		 */

		cale.add(Calendar.MONTH, 1);

		cale.set(Calendar.DAY_OF_MONTH, 0);

		String lastDayOfMonth = formatter.format(cale.getTime()); // 当月最后一天 2019-02-28
		return sdfDay.format(cale.getTime());
	}

	public static boolean timeCalendar(Date nowTime, Date amBeginTime, Date amEndTime) {
		// 设置当前时间
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);
		// 设置开始时间
		Calendar amBegin = Calendar.getInstance();
		amBegin.setTime(amBeginTime);// 上午开始时间
		// 设置结束时间
		Calendar amEnd = Calendar.getInstance();
		amEnd.setTime(amEndTime);// 上午结束时间
		// 处于开始时间之后，和结束时间之前的判断
		if ((date.after(amBegin) && date.before(amEnd))) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取本周的第一天
	 *
	 * @return String
	 **/
	public static String getWeekStart(int i) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		Calendar cal = Calendar.getInstance();
		// Calendar c = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -i);
		// cal.setTime(time);

		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一

		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		/*
		 * cal.add(Calendar.DATE, 6);
		 * System.out.println("所在周星期日的日期："+sdf.format(cal.getTime()));
		 */

		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()) + " 00:00:00";
	}

	/**
	 * 获取本周的最后一天
	 *
	 * @return String
	 **/
	public static String getWeekEnd(int i) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 设置时间格式
		Calendar cal = Calendar.getInstance();
		// Calendar c = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -i);
		// cal.setTime(time);

		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}

		cal.setFirstDayOfWeek(Calendar.MONDAY);// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一

		int day = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值

		cal.add(Calendar.DATE, 6);

		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()) + " 23:59:59";
	}

	public static Map<String,String> getNexPeriod(String daystr,String period) {
		Map<String,String> map = new HashMap<String,String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = sdf.parse (daystr);
			Calendar cal1 = Calendar.getInstance();
			Calendar cal2 = Calendar.getInstance();
			switch (period) {
				case "specialPeriod001"://每日
					cal1.setTime(date);
					cal1.add(Calendar.DATE, 1);
					map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
					map.put("PERIOD_END_DATE",sdf.format(cal1.getTime()));
					break;
				case "specialPeriod002"://每周
					cal1.setTime(date);
					cal2.setTime(date);
					int dayWeek = cal1.get(Calendar.DAY_OF_WEEK);
					if(dayWeek == 1){
						cal1.add(Calendar.DAY_OF_MONTH, 0);
						cal2.add(Calendar.DAY_OF_MONTH, 6);
					} else {
						cal1.add(Calendar.DAY_OF_MONTH, 1-dayWeek+7);
						cal2.add(Calendar.DAY_OF_MONTH, 1-dayWeek+13);
					}
					map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
					map.put("PERIOD_END_DATE",sdf.format(cal2.getTime()));
					break;
				case "specialPeriod003"://每旬
					Calendar now = Calendar.getInstance();
					now.setTime(date);
					if(now.get(Calendar.DATE)<10){
						cal1.setTime(date);
						cal2.setTime(date);
						cal1.set(Calendar.DAY_OF_MONTH,11);
						cal2.set(Calendar.DAY_OF_MONTH,20);
						map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
						map.put("PERIOD_END_DATE",sdf.format(cal2.getTime()));
					}else if(now.get(Calendar.DATE)<20){
						cal1.setTime(date);
						cal2.setTime(date);
						cal1.set(Calendar.DAY_OF_MONTH,21);
						cal2.set(Calendar.DAY_OF_MONTH, cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
						map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
						map.put("PERIOD_END_DATE",sdf.format(cal2.getTime()));
					}else{
						cal1.setTime(date);
						cal2.setTime(date);
						cal1.add(Calendar.MONTH, 1);
						cal2.add(Calendar.MONTH, 1);
						cal1.set(Calendar.DAY_OF_MONTH,1);
						cal2.set(Calendar.DAY_OF_MONTH, 10);
						map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
						map.put("PERIOD_END_DATE",sdf.format(cal2.getTime()));
					}
					break;
				case "specialPeriod004"://每月
					cal1.setTime(date);
					cal2.setTime(date);
					cal1.set(Calendar.DAY_OF_MONTH,1);
					cal1.add(Calendar.MONTH, 1);
					cal2.add(Calendar.MONTH, 1);
					cal2.set(Calendar.DAY_OF_MONTH, cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
					map.put("PERIOD_START_DATE",sdf.format(cal1.getTime()));
					map.put("PERIOD_END_DATE",sdf.format(cal2.getTime()));
					break;
				default:
					break;
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return map;
		}

		return map;
	}

	/**
	 * 获取开始日期和结束日期之间的所有日期，包括边界
	 *
	 * @return 日期字符串列表
	 */
	public static List<String> getDatesInRange(String startDateStr, String endDateStr) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime startDate = LocalDateTime.parse(startDateStr, formatter);
		LocalDateTime endDate = LocalDateTime.parse(endDateStr, formatter);
		int daysBetween = (int) ChronoUnit.DAYS.between(startDate, endDate.plusDays(1));
		List<String> datesInRange = new ArrayList<>(daysBetween);
		for (int i = 0; i < daysBetween; i++) {
			datesInRange.add(startDate.plusDays(i).format(DateTimeFormatter.ISO_LOCAL_DATE));
		}
		return datesInRange;
	}


	/**
	 * 判断两个日期时间是否属于同一个月份。
	 *
	 * @param date1 第一个日期时间字符串，格式为 yyyy-MM-dd HH:mm:ss。
	 * @param date2 第二个日期时间字符串，格式为 yyyy-MM。
	 * @return 如果两个日期时间属于同一个月份，则返回 true，否则返回 false。
	 */
	public static boolean isSameMonth(String date1, String date2) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime dateTime1 = LocalDateTime.parse(date1, formatter);
		LocalDateTime dateTime2 = LocalDateTime.parse(date2 + "-01 00:00:00", formatter);
		return dateTime1.getYear() == dateTime2.getYear() && dateTime1.getMonthValue() == dateTime2.getMonthValue();
	}

	/**
	 * 获取指定年份指定月的天数
	 *
	 * @return
	 */
	public static int getMonthDays(int year,int month) {

		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.YEAR, year);
		cale.set(Calendar.MONTH, month);
		return cale.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 *获取当月第一天
	 */
	public static LocalDateTime getFirstLocalDayOfMonth(LocalDateTime localDateTime) {
		return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
	}

	/**
	 *获取当月第一周  以第一个周一为准
	 */
	private static LocalDateTime getFirstMonday(LocalDateTime sourceTime) {
		LocalDateTime firstMondayOfMonth = getFirstLocalDayOfMonth(sourceTime);
		for (int i = 0; i < 6; i++) {
			DayOfWeek dayOfWeekTemp = firstMondayOfMonth.getDayOfWeek();
			if (dayOfWeekTemp.equals(DayOfWeek.MONDAY)) {
				break;
			}
			//往后推一天
			firstMondayOfMonth = firstMondayOfMonth.plusDays(1);
		}
		return firstMondayOfMonth;
	}


	/**
	 * 获取几月份的第几周 [X月份第X周]
	 *
	 */
	public static String getMonthNoAndWeekNo(String date) {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime sourceTime = LocalDateTime.parse(date, formatter);
		String monthNoAndWeekNo;
		//获取当月的第一天
		//获取月第一个周一,从当月第一天开始找

		LocalDateTime firstMondayOfMonth = getFirstMonday(sourceTime);

		//比较当月的第一个星期一 < = 参数时间
		if (firstMondayOfMonth.isBefore(sourceTime) || firstMondayOfMonth.isEqual(sourceTime)) {
			//当月第一个周一在当前时间之前 firstMondayOfMonth<=sourceTime
			//计算两个时间间隔天数
			int dayOfMonthFirstMonday = firstMondayOfMonth.getDayOfMonth();
			int dayOfMonthSourceTime = sourceTime.getDayOfMonth();

			int diffDays = dayOfMonthSourceTime - dayOfMonthFirstMonday;
			//第几周weekNo
			int weekNo = (diffDays / 7) + 1;
			//月份
			int monthNo = sourceTime.getMonth().getValue();

//			monthNoAndWeekNo = monthNo + "月份" + "第" + weekNo + "周";

			monthNoAndWeekNo = sourceTime.getYear() + "," + (monthNo <= 9 ? "0"+monthNo : monthNo) + "," + weekNo;
		} else {
			//如果当月的第一个周一大于参数时间,则要计算到上个月份去
			//获取上一个月的第一个周一
			LocalDateTime lastMontLocalDateTime = sourceTime.minusMonths(1);
			//上个月的第一天
			//从上个月的第一天开始找周一
			LocalDateTime firstMondayOfMonthLast = getFirstMonday(lastMontLocalDateTime);

			//  计算两个时间间隔天数 (上月第一个周一 减去 当前时间)
			Duration duration = Duration.between(firstMondayOfMonthLast, sourceTime);
			long diffDays = duration.toDays(); //相差的天数
			//第几周weekNo
			long weekNo = (diffDays / 7) + 1;
			//月份
			int monthNo = firstMondayOfMonthLast.getMonth().getValue();//汉字版月份

//			monthNoAndWeekNo = monthNo + "月份" + "第" + weekNo + "周";

			monthNoAndWeekNo = sourceTime.getYear() + "," + (monthNo <= 9 ? "0"+monthNo : monthNo) + "," + weekNo;
		}
		return monthNoAndWeekNo;
	}

	public static void main(String[] args) throws ParseException {
//		System.out.println(getDatesInRange("2023-02-08 00:00:00","2023-02-08 23:59:59"));
//		for (int i = 0; i < 12; i++) {
//			System.out.println((i+1)+"月份有"+getMonthDays(2023,i)+"天");
//		}
//		for (int i = 0; i < 12; i++) {
//			int days = DateSysUtil.getMonthDays(2023, i);
//			for (int j = 1; j <= days; j++) {
//				String curDay = 2023 + "-" + ((i+1) <= 9 ? "0"+(i+1) : (i+1)) + "-" + (j <= 9 ? ("0"+j) : j);
//				System.out.println(curDay);
//			}
//
//		}
//		System.out.println("2023-11-21 16:29:59".substring(0,10));
//		System.out.println(DateSysUtil.fomatDate("2023-11-21").after(DateSysUtil.fomatDate("2023-11-20")));
//		System.out.println(DateSysUtil.fomatDate("2023-11-21").after(DateSysUtil.fomatDate("2023-11-21")) || DateSysUtil.fomatDate("2023-11-21").equals(DateSysUtil.fomatDate("2023-11-21")));
//		System.out.println(DateSysUtil.fomatDate("2023-11-21").after(DateSysUtil.fomatDate("2023-11-22")));

		System.out.println(getMonthNoAndWeekNo("2023-12-04 00:00:00"));
	}

}
