package com.ibeetl.admin.flow.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 日期工具类
 */

public class DateUtil {
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String YYYY_MM_DD_HH_MM_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	private static final Log log = LogFactory.getLog(DateUtil.class);

	/**
	 *按指定格式将字符串转换为Timestamp格式
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Timestamp to_timestamp(String dateStr, String format) {
		try{
			DateFormat df = new SimpleDateFormat(format);
			Date date = df.parse(dateStr);
			Timestamp d = new Timestamp(date.getTime());

			return d;
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 获取当前时间的中文显示：yyyy年MM月dd日 HH时:mm分:ss秒
	 *
	 * @return
	 */
	public static String nowCn() {
		return new SimpleDateFormat("yyyy年MM月dd日 HH时:mm分:ss秒")
				.format(new Date());
	}

	/**
	 * 获取当前时间的英文显示：yyyy-MM-dd HH:mm:ss
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowEn() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
				System.currentTimeMillis()));
	}

	/**
	 * 获取当前时间的无间隔符的显示：yyyyMMddHHmmss
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowEns() {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}

	/**
	 * 获取当前时间的年月：yyyyMM
	 * @return
	 */
	public static String nowEnsDate() {
		return new SimpleDateFormat("yyyyMM").format(new Date());
	}

	/**
	 * 将Date格式参数转成String格式的yyyyMMddHHmmss
	 *
	 * @param date
	 * @return
	 */
	public static String nowEns(Date date) {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}

	/**
	 * 将Date参数转成指定格式的字符串
	 *
	 * @param date
	 * @return
	 */
	public static String nowEns(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	/**
	 * String转成次格式的date
	 *
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public static Date getDate(String strDate, String format) throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(strDate);
		return date;
	}

	/**
	 * String转成次格式的String
	 *
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public static String getStringto(String strDate, String format)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(strDate);

		String dateStr = new SimpleDateFormat(format).format(date);
		return dateStr;
	}
	/**
	 * String转成次格式的String
	 *
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public static String getStringto2(String strDate, String format,String newformat)
			throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = sdf.parse(strDate);

		String dateStr = new SimpleDateFormat(newformat).format(date);
		return dateStr;
	}

	/**
	 * String转成次格式的String
	 *
	 * @param strDate
	 * @return
	 */
	public static String getStringto(Date strDate, String format) {

		String dateStr = new SimpleDateFormat(format).format(strDate);
		return dateStr;
	}

	/**
	 * 将当前时间减去或加上多长时间
	 *
	 * @param amount
	 *            （-2：代表当前时间减去两小时）
	 * @return
	 */
	public static String nowEns(int amount) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, amount);
		Date date = c.getTime();
		return DateUtil.nowEns(date);
	}

	/**
	 * 获取当前日期的中文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateCn() {
		return new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
	}

	/**
	 * 获取当前日期的英文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateEn() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}

	/**
	 * 获取当前年和月的英文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowMonthDateEn() {
		return new SimpleDateFormat("yyyy-MM").format(new Date());
	}

	/**
	 * 获取当前日期显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateEn(String formnat) {
		return new SimpleDateFormat(formnat).format(new Date());
	}

	/**
	 * 获取昨天的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String yesterDateEn() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}
	/**
	 * 获取前两天的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String twoyesterDateEn() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -2);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}

	/**
	 * 获取昨天的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String yesterDateEn2() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取明天的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String tomorrowDateEn() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
	}

	/**
	 * 获取明天的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String tomorrowDateEn2() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取明天起始时间戳，如今天是 20160701，则返回 20160702000000
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String tomorrowStartTimeStampEn2() {
		String startTime = "000000";
		return tomorrowDateEn2() + startTime;
	}

	/**
	 * 获取明天起始时间戳，如今天是 20160701，则返回 2016-07-02 00:00:00
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String tomorrowStartTimeStampEn() {
		String startTime = " 00:00:00";
		return tomorrowDateEn() + startTime;
	}

	/**
	 * 获取当前日期的yyyyMMdd显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateyyyyMMdd() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}

	/**
	 * 获取当前日期的yyyyMM显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateyyyyMM() {
		return new SimpleDateFormat("yyyyMM").format(new Date());
	}

	/**
	 * 获取yyyyMM上月
	 * @return
	 */
	public static String lastMonthDateyyyyMM() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		Date dt = new Date();
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, -1);//
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;
	}

	/**
	 * 获取当前日期的yyyy-MM-dd HH:mm:ss显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateyyyyMMdd2() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	/**
	 * 日期转换成字符串，如果需转换的日期为NULL，则返回为NULL（默认格式为日期）
	 *
	 * @param date
	 *            Date 需转换的日期
	 * @return String 日期字符串
	 */
	public static String formatMedium(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DatePatten.yyyyMMdd);
		return sdf.format(date);
	}

	/**
	 * 获取当前时间的中文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowTimeCn() {
		return new SimpleDateFormat("HH时:mm分:ss秒").format(new Date());
	}

	/**
	 * 获取当前时间的英文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowTimeEn() {
		return new SimpleDateFormat("HH:mm:ss").format(new Date());
	}

	/**
	 * 获取当前时间的英文显示
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String getNowDate() {
		return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
	}

	/**
	 * 获取某年某月第一天日期
	 *
	 * @param year
	 *            年
	 * @param month
	 *            月份
	 * @param format
	 *            显示格式
	 * @return
	 * @author LiMingyu
	 */
	public static String getCurrMonthFirst(int year, int month, String format) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
		return new SimpleDateFormat(format).format(cal.getTime());
	}

	/**
	 * 获取某年某月最后一天日期
	 *
	 * @param year
	 *            年
	 * @param month
	 *            月份
	 * @param format
	 *            显示格式
	 * @return
	 * @author LiMingyu
	 */
	public static String getCurrMonthLast(int year, int month, String format) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, value);
		return new SimpleDateFormat(format).format(cal.getTime());
	}

	/**
	 * 获取当前月向前X个月的list
	 *
	 * @param count
	 *            向前推的月份数量
	 * @return
	 * @author LiMingyu
	 */
	public static List<Map<String, Object>> getPerMonth(int count) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < count; i++) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -i);
			Map<String, Object> map = new HashMap<String, Object>();
			int year = cal.get(Calendar.YEAR);
			int month = (cal.get(Calendar.MONTH) + 1);
			map.put("formatStr", year + "年" + month + "月");
			map.put("year", year);
			map.put("month", month);
			map.put("yyyyMM", String.valueOf(year)
					+ (month < 10 ? "0" + month : month));
			list.add(map);
		}
		return list;
	}

	/**
	 * 获取当前月向前X个月的list
	 *
	 * @param count
	 *            向前推的月份数量
	 * @return
	 * @author LiMingyu
	 */
	public static List<Map<String, Object>> getMonthFromPerMonth(int count) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 1; i < count + 1; i++) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -i);
			Map<String, Object> map = new HashMap<String, Object>();
			int year = cal.get(Calendar.YEAR);
			int month = (cal.get(Calendar.MONTH) + 1);
			map.put("formatStr", year + "年" + month + "月");
			map.put("year", year);
			map.put("month", month);
			map.put("yyyyMM", String.valueOf(year)
					+ (month < 10 ? "0" + month : month));
			list.add(map);
		}
		return list;
	}

	/**
	 * 获取当前毫秒数
	 */
	public static long getNowDateTime() {
		return (new Date()).getTime();
	}

	/**
	 * 返回时间戳对应的 时间戳字符串
	 *
	 * @param timestamp
	 * @return
	 * @author Limingyu
	 */
	public static String timstamp2String(Timestamp timestamp) {
		if (timestamp == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定义格式，不显示毫秒
		String str = df.format(timestamp);
		if (str != null && str.length() > 10)
			str = str.substring(0, 10);
		return str;
	}

	/**
	 * 返回时间戳对应的日期字符串
	 *
	 * @param timestamp
	 * @return
	 * @author LiMingyu
	 */
	public static String timstamp3String(Timestamp timestamp) {
		if (timestamp == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 定义格式，不显示毫秒
		String str = df.format(timestamp);
		if (str != null && str.length() > 10)
			str = str.substring(0, 10);
		return str;
	}

	/**
	 * 返回时间戳字符串对应的时间戳
	 *
	 * @param timestamp
	 * @return
	 * @author LiMingyu
	 */
	public static Timestamp string2Datestamp(String timestamp) {
		if (timestamp == null || timestamp.length() < 10)
			return null;
		Timestamp ts = Timestamp.valueOf(timestamp.trim() + " 00:00:00");
		return ts;
	}

	/**
	 * 格式化日期串
	 *
	 * @param formatStr
	 * @return
	 * @author LiMingyu
	 */
	public static String getFormatDate(Date date, String formatStr) {
		if (formatStr == null || null == date) {
			return "";
		} else {
			return new SimpleDateFormat(formatStr).format(date);
		}
	}

	/**
	 * 当前月份
	 */
	public static String getCurrentDateMonth_(String format) {
		// e.g. format="yyyyMM";
		return getFormatDate(new Date(), format);
	}

	/**
	 * 获取当月的首日和末日 例如： 2014-09-01 2014-09-30
	 *
	 * @param
	 */
	public static String getMothFday() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		// 当前月的第一天
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		Date beginTime = cal.getTime();
		return datef.format(beginTime);
	}

	/**
	 * 获取当月的首日和末日 例如： 2014-09-01 2014-09-30
	 *
	 * @param date
	 */
	public static String getMothFday(String date) {
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(datef.parse(date));
			cal.set(GregorianCalendar.DAY_OF_MONTH, 1);// 当前月的第一天
			Date beginTime = cal.getTime();
			return datef.format(beginTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getMothLday() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		// 当前月的最后一天
		cal.set(Calendar.DATE, 1);
		cal.roll(Calendar.DATE, -1);
		Date endTime = cal.getTime();
		return datef.format(endTime);
	}

	public static String getMothLday(String date) {
		SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(datef.parse(date));
			cal.set(Calendar.DATE, 1);// 当前月的最后一天
			cal.roll(Calendar.DATE, -1);
			Date endTime = cal.getTime();
			return datef.format(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 传入时间戳 返回YYYYMM
	 *
	 * @param timestamp
	 * @return
	 * @author LiMingyu
	 */
	public static String timstamp4String(Timestamp timestamp) {
		if (timestamp == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");// 定义格式，不显示毫秒
		String str = df.format(timestamp);
		if (str != null && str.length() > 10)
			str = str.substring(0, 10);
		return str;
	}

	/**
	 * 获取当前时间戳
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static Timestamp getTimestamp() {
		return new Timestamp(System.currentTimeMillis());

	}

	/**
	 * 获取当天日终时间
	 *
	 * @return
	 */
	public static String getEndtime() {
		return DateUtil.nowEns().substring(0, 8) + "235959";
	}

	/**
	 * 获取参数日期日终时间
	 *
	 * @return
	 */
	public static String getEndtime(Date ordertime) {
		String time = nowEns(ordertime);
		return time.substring(0, 8) + "235959";
	}

	/**
	 * 获取当前毫秒数 currentTimeMillis
	 *
	 * @return
	 */
	public static String getTimeMillis() {
		return ((Long) System.currentTimeMillis()).toString();
	}

	/**
	 * 计算两个参数时间相差多久
	 *
	 * @param stardate
	 * @param endtime
	 * @return
	 */
	public static long mathday(Date stardate, Date endtime) {

		return (endtime.getTime() - stardate.getTime());
	}

	/**
	 * 计算两个参数时间相差多久
	 *
	 * @param stardate
	 * @param enddate
	 * @return
	 * @throws Exception
	 */
	public static long mathday(String stardate, String enddate, String format)
			throws Exception {

		Date starttime = DateUtil.getDate(stardate, format);
		Date endtime = DateUtil.getDate(enddate, format);
		return (endtime.getTime() - starttime.getTime());
	}

	/**
	 * 计算两个参数时间相差多久
	 *
	 * @param stardate
	 * @param enddate
	 * @return
	 * @throws Exception
	 */
	public static long mathdays(String stardate, String enddate, String format)
			throws Exception {

		Date starttime = DateUtil.getDate(stardate, format);
		Date endtime = DateUtil.getDate(enddate, format);
		long len = endtime.getTime() - starttime.getTime();
		long day = len/1000/60/60/24;

		return day;
	}

	/**
	 * 获取当前月的上num个月
	 *
	 * @return
	 */
	public static String getLastMonth(int num) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, num);
		return new SimpleDateFormat("yyyy-MM").format(c.getTime());
	}

	/**
	 * 获取当前月的上num个月(指定格式)
	 *
	 * @return
	 */
	public static String getLastMonth(int num, String format) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, num);
		return new SimpleDateFormat(format).format(c.getTime());
	}

	public static Date getDateStart(Date date) throws ParseException {
		DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = f.format(date);
		return f.parse(s);
	}

	public static Date getDateEnd(Date date) throws ParseException {
		DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String s = f.format(date);
		return f.parse(s + " 23:59:59");
	}

	public static String nowDateyyyy_MM() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
	}

	// 获取本周周一，周日
	public static Map<String, String> nowDateyyyy_MM_Week() {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.DAY_OF_WEEK, 2);
		calendar1.add(Calendar.DAY_OF_MONTH, -1);
		Calendar stcalendarMonth = Calendar.getInstance();
		stcalendarMonth.set(Calendar.MONTH,
				Integer.parseInt(new SimpleDateFormat("MM").format(calendar
						.getTime())) - 2);
		stcalendarMonth.set(Calendar.DAY_OF_MONTH,
				Integer.parseInt(new SimpleDateFormat("dd").format(calendar
						.getTime())));

		stcalendarMonth.set(Calendar.YEAR,
				Integer.parseInt(new SimpleDateFormat("yyyy").format(calendar
						.getTime())) - 0);
		Calendar edcalendarMonth = Calendar.getInstance();
		edcalendarMonth.set(Calendar.MONTH,
				Integer.parseInt(new SimpleDateFormat("MM").format(calendar1
						.getTime())) - 2);
		edcalendarMonth.set(Calendar.DAY_OF_MONTH,
				Integer.parseInt(new SimpleDateFormat("dd").format(calendar1
						.getTime())));
		map.put("monday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())
						+ " 00:00:00");
		map.put("sunday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime())
						+ " 23:59:59");
		map.put("pmonthstarttime",
				new SimpleDateFormat("yyyy-MM-dd").format(stcalendarMonth
						.getTime()) + " 00:00:00"); //上月同期开始时间
		map.put("pmonthendtime",
				new SimpleDateFormat("yyyy-MM-dd").format(edcalendarMonth
						.getTime()) + " 23:59:59");//上月同期结束时间
		return map;
	}

	// 获取上周周一，周日
	public static Map<String, String> proDateyyyy_MM_Week() {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		calendar.add(Calendar.DAY_OF_MONTH, -14);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.DAY_OF_WEEK, 2);
		calendar1.add(Calendar.DAY_OF_MONTH, -8);
		map.put("monday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())
						+ " 00:00:00");
		map.put("sunday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime())
						+ " 23:59:59");
		return map;
	}

	// 获取上周周一，周日
	public static Map<String, String> DateY_M_DAndPro_Y_M_D(Integer count) {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		calendar.add(Calendar.DAY_OF_MONTH, -7 + count);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.DAY_OF_WEEK, 2);
		calendar1.add(Calendar.DAY_OF_MONTH, 0 + count);
		map.put("starttime",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime())
						+ " 00:00:00");
		map.put("pwstarttime",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime())
						+ " 00:00:00");
		return map;
	}

	public static Map<String, String> OnYear() {
		String[] datearr = yesterDateEn().split("-");
		Integer month = Integer.parseInt(datearr[1]);
		Map<String, String> map = new HashMap<String, String>();
		if (month > 1 && month <= 10) {
			map.put("dmonth", datearr[0] + "-0" + (month - 1));
			map.put("dday", datearr[0] + "-0" + (month - 1) + "-" + datearr[2]);
		} else if (month == 1) {
			map.put("dmonth", datearr[0] + "-12");
			map.put("dday", datearr[0] + "-12" + "-" + datearr[2]);
		} else if (month <= 12 && month > 10) {
			map.put("dmonth", datearr[0] + "-" + (month - 1));
			map.put("dday", datearr[0] + "-" + (month - 1) + "-" + datearr[2]);
		}
		return map;
	}

	public static String[] getweekday() {
		String[] arr = new String[] { "", "", "", "", "", "", "" };
		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		Calendar calendar3 = Calendar.getInstance();
		Calendar calendar4 = Calendar.getInstance();
		Calendar calendar5 = Calendar.getInstance();
		Calendar calendar6 = Calendar.getInstance();
		Calendar calendar7 = Calendar.getInstance();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		calendar1.add(Calendar.DAY_OF_MONTH, -1);
		arr[0] = sf.format(calendar1.getTime());
		calendar2.add(Calendar.DAY_OF_MONTH, -2);
		arr[1] = sf.format(calendar2.getTime());
		calendar3.add(Calendar.DAY_OF_MONTH, -3);
		arr[2] = sf.format(calendar3.getTime());
		calendar4.add(Calendar.DAY_OF_MONTH, -4);
		arr[3] = sf.format(calendar4.getTime());
		calendar5.add(Calendar.DAY_OF_MONTH, -5);
		arr[4] = sf.format(calendar5.getTime());
		calendar6.add(Calendar.DAY_OF_MONTH, -6);
		arr[5] = sf.format(calendar6.getTime());
		calendar7.add(Calendar.DAY_OF_MONTH, -7);
		arr[6] = sf.format(calendar7.getTime());
		return arr;
	}

	public static Boolean isValidDate(String str) {
		boolean convertSuccess = true;
		// 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
//		Calendar c = Calendar.getInstance();
		try {
			// 设置lenient为false.
			// 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
			format.setLenient(false);
			Date date = format.parse(str);
			String string = str.substring(4);
			if(string.length() !=2){
				convertSuccess = false;
			}else {
				int inmonth = Integer.parseInt(str.substring(4));//截取传入字符串的月份
//				log.info(month);
				if (inmonth >= 1 && inmonth <= 12) {
					convertSuccess = true;
				}else{
					convertSuccess = false;
				}
			}


		} catch (ParseException e) {
			e.printStackTrace();
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			convertSuccess = false;
		}
		return convertSuccess;

	}

	/**
	 * 计算月份+i
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static String addMonth(String time, int i) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date dt = sdf.parse(time);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, i);// 日期加1个月
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;

	}

	/**
	 * 计算月份+i
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static String addMonth2(String time, int i) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date dt = sdf.parse(time);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, i);// 日期加1个月
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;

	}
	/**
	 * 计算月份-i
	 *
	 * @param time
	 * @return
	 * @throws Exception
	 */
	public static String offMonth2(String time, int i) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date dt = sdf.parse(time);
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(dt);
		rightNow.add(Calendar.MONTH, -i);// 日期加1个月
		Date dt1 = rightNow.getTime();
		String reStr = sdf.format(dt1);
		return reStr;

	}
	public static int difMonth(String time1, String time2) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		bef.setTime(sdf.parse(time1));
		aft.setTime(sdf.parse(time2));
		int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
		int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
		System.out.println(Math.abs(month + result));
		return month + result;
	}
	public static int difMonth(String time1, String time2,String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		try {
			bef.setTime(sdf.parse(time1));
			aft.setTime(sdf.parse(time2));
			int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
			int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
//			System.out.println(Math.abs(month + result));
			return month + result;
		} catch (ParseException e) {

			e.printStackTrace();
			return e.getErrorOffset();
		}

	}

	// 上月第一天
	public static String lastMonthStart() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.MONTH, -1);
		cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = format.format(cal_1.getTime());
		System.out.println("-----1------firstDay:" + firstDay);
		return firstDay;
	}

	// 上月最后一天
	public static String lastMonthEnd() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}

	public static String lastMonth() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}

	// 上月最后一天
	public static String lastMonthEndNow() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, cale.getActualMaximum(Calendar.DAY_OF_MONTH));// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime())+"235959";
		return lastDay;
	}
	// 前两月第一天
	public static String twoMonthStart() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_2 = Calendar.getInstance();// 获取当前日期
		cal_2.add(Calendar.MONTH, -2);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay2 = format.format(cal_2.getTime());
		return firstDay2;
	}

	// 前两月最后一天
	public static String twoMonthEnd() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale2 = Calendar.getInstance();
		cale2.add(Calendar.MONTH, -1);
		cale2.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay2 = format.format(cale2.getTime());
		System.out.println("-----2------lastDay:" + lastDay2);
		return lastDay2;
	}
	// 后两月最后一天
	public static String tonMonthEnd() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale2 = Calendar.getInstance();
		cale2.add(Calendar.MONTH, 2);
		cale2.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay2 = format.format(cale2.getTime());
		System.out.println("-----2------lastDay:" + lastDay2);
		return lastDay2;
	}

	// 获取本周周一，周日 yyyy-mm-dd
	public static Map<String, String> nowY_M_DWeek() {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		calendar.add(Calendar.DAY_OF_MONTH, -7);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.DAY_OF_WEEK, 2);
		calendar1.add(Calendar.DAY_OF_MONTH, -1);
		Calendar stcalendarMonth = Calendar.getInstance();
		stcalendarMonth.set(Calendar.YEAR,
				Integer.parseInt(new SimpleDateFormat("yyyy").format(calendar
						.getTime())) - 0);
		stcalendarMonth.set(Calendar.MONTH,
				Integer.parseInt(new SimpleDateFormat("MM").format(calendar
						.getTime())) - 2);
		stcalendarMonth.set(Calendar.DAY_OF_MONTH,
				Integer.parseInt(new SimpleDateFormat("dd").format(calendar
						.getTime())));
		Calendar edcalendarMonth = Calendar.getInstance();
		edcalendarMonth.set(Calendar.MONTH,
				Integer.parseInt(new SimpleDateFormat("MM").format(calendar1
						.getTime())) - 2);
		edcalendarMonth.set(Calendar.DAY_OF_MONTH,
				Integer.parseInt(new SimpleDateFormat("dd").format(calendar1
						.getTime())));
		map.put("monday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
		map.put("sunday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime()));
		map.put("pmonthstarttime", new SimpleDateFormat("yyyy-MM-dd")
				.format(stcalendarMonth.getTime()));
		map.put("pmonthendtime", new SimpleDateFormat("yyyy-MM-dd")
				.format(edcalendarMonth.getTime()));
		return map;
	}

	// 获取上周周一，周日 yyyy-mm-dd
	public static Map<String, String> pronowY_M_DWeek() {
		Map<String, String> map = new HashMap<String, String>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, 2);
		calendar.add(Calendar.DAY_OF_MONTH, -14);
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(Calendar.DAY_OF_WEEK, 2);
		calendar1.add(Calendar.DAY_OF_MONTH, -8);
		map.put("monday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
		map.put("sunday",
				new SimpleDateFormat("yyyy-MM-dd").format(calendar1.getTime()));
		return map;
	}
	/**
	 * 获取现在的年份
	 * @return
	 */
	public static String nowYearsyyyy() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return new SimpleDateFormat("yyyy").format(calendar.getTime());
	}
	/**
	 * 获取当前的月份
	 * @return
	 */
	public static Integer nowMonthM() {
		return Integer.parseInt(new SimpleDateFormat("M").format(new Date(
				System.currentTimeMillis())));
	}

	public static String lastMonthEnd_d() {
		SimpleDateFormat format = new SimpleDateFormat("d");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}
	public static String lastMonthEndyyyy_M() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-M");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 0);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}
	public static String lastMonthEndyyyy_MN() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-M");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}

	/**
	 * 获取昨天早上9点的时间的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String yesterDateTimeEn() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return new SimpleDateFormat("yyyy-MM-dd 09:00:00").format(calendar.getTime());
	}
	/**
	 * 获取今天早上9点的时间的日期
	 *
	 * @return
	 * @author LiMingyu
	 */
	public static String nowDateTimeEn() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 0);
		return new SimpleDateFormat("yyyy-MM-dd 09:00:00").format(calendar.getTime());
	}
	public static ArrayList<LinkedHashMap<String, String>> getLastMonthFromPerMonth(int count) {
		ArrayList<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
		for (int i = 1; i < count + 1 ; i++) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -i);
			LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			int year = cal.get(Calendar.YEAR);
			int month = (cal.get(Calendar.MONTH) + 1);
			map.put("formatStr", year + "年" + month + "月");
			map.put("year", Integer.toString(year));
			map.put("month", Integer.toString(month));
			map.put("yyyy-MM", String.valueOf(year)
					+ "-" + (month < 10 ? "0" + month : month));
			list.add(map);
		}
		return list;
	}

	public static ArrayList<LinkedHashMap<String, String>> getLastMonthFromPerMonth2(int count) {
		ArrayList<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
		String m = "m";
		for (int i = 1; i < count + 1 ; i++) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -i);
			LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
			int year = cal.get(Calendar.YEAR);
			int month = (cal.get(Calendar.MONTH) + 1);


			map.put("yyyy-MM", String.valueOf(year)
					+ "-" + (month < 10 ? "0" + month : month));

			list.add(map);
		}







		return list;
	}

	//当月第一天
	public static String thisMonthStart() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_1 = Calendar.getInstance();// 获取当前日期
		cal_1.add(Calendar.MONTH, 0);
		cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = format.format(cal_1.getTime());
//			System.out.println("-----1------firstDay:" + firstDay);
		return firstDay;
	}


	//获取传入的开始时间到结束时间的每月
	public static ArrayList<LinkedHashMap<String, String>> getLastMonthFromPerMonth3(String start,String end) {
		ArrayList<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
//	        List<String> betweenList = new ArrayList<String>();

		try{
			Calendar startDay = Calendar.getInstance();
			startDay.setTime(format.parse(end));
			startDay.add(Calendar.MONTH, 1);

			while(true){
				startDay.add(Calendar.MONTH, -1);
				Date newDate = startDay.getTime();
				String newend=format.format(newDate);
				LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
				map.put("yyyyMM",newend);
				list.add(map);
				if(start.equals(newend)){
					break;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

		return list;

	}

	public static ArrayList<String> getLastMonthFromPerMonth4(String start,String end) {
		ArrayList<String> list = new ArrayList<String>();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
//	        List<String> betweenList = new ArrayList<String>();

		try{
			Calendar startDay = Calendar.getInstance();
			startDay.setTime(format.parse(end));
			startDay.add(Calendar.MONTH, 1);

			while(true){
				startDay.add(Calendar.MONTH, -1);
				Date newDate = startDay.getTime();
				String newend=format.format(newDate);
				list.add(newend);
				if(start.equals(newend)){
					break;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}

		return list;

	}

	/**
	 * 获取传入日期的上一个月
	 * @return
	 */
	public static String lastMonthEndyyyy_MNByDate(String str) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		// 获取前月的最后一天
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String lastDay = format.format(cale.getTime());
		return lastDay;
	}


	/**
	 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 *
	 * @param nowTime 当前时间
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime()
				|| nowTime.getTime() == endTime.getTime()) {
			return true;
		}

		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取当前月的上num个月(指定格式)
	 *
	 * @return
	 */
	public static Date getLastMonthDate(int num) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, num);
		Date time = c.getTime();
		return time;
	}

	/**
	 * 获取传入时间的上*年*月*日
	 * @param dateStr
	 * @param addYear
	 * @param addMonth

	 * @return
	 * @throws Exception
	 */
	public static String getLastDate(String dateStr,int addYear, int addMonth) throws Exception {

		try {

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

			Date sourceDate = sdf.parse(dateStr);

			Calendar cal = Calendar.getInstance();

			cal.setTime(sourceDate);

			cal.add(Calendar.YEAR,addYear);

			cal.add(Calendar.MONTH, addMonth);

//			cal.add(Calendar.DATE, addDate);

			SimpleDateFormat returnSdf = new SimpleDateFormat("yyyy-MM");

			String dateTmp = returnSdf.format(cal.getTime());

			Date returnDate = returnSdf.parse(dateTmp);

			return dateTmp;

		} catch (Exception e) {

			e.printStackTrace();

			throw new Exception(e.getMessage());

		}

	}

	@Autowired
	public static void main(String[] args) {
		try {
			System.out.println("将Date格式参数转成String格式的yyyyMMddHHmmss:"+nowEns(new Date()));
			System.out.println("当前时间减去几个小时：" + nowEns(-2));
			System.out.println("当前时间加上几个小时：" + nowEns(2));
			System.out.println("获取昨天的日期：" + yesterDateEn());
			System.out.println(getCurrMonthFirst(2022,4,"yyyy-MM-dd HH:mm:ss"));
			System.out.println(getCurrMonthLast(2022,4,"yyyy-MM-dd HH:mm:ss"));
			System.out.println("当月的前几个月的list：" + getMonthFromPerMonth(6));
			System.out.println("当前毫秒数：" + getNowDateTime());
			System.out.println(getLastDate("2022-01-30",-1,-1));
			String str = "2022-05-23 23:59:59";
			System.out.println("获取上月同期:"+addMonth(str.substring(0,7),-1)+str.substring(7));
			System.out.println("获取去年同期:"+addMonth(str.substring(0,7),-12)+str.substring(7));
			System.out.println("获取上月格式yyyyMM:"+lastMonthDateyyyyMM());
			System.out.println("获取两个时间的每月=" + getLastMonthFromPerMonth3("202201","202205")+"len="+getLastMonthFromPerMonth3("202201","202205").size());

			System.out.println("获取传入的开始账期到结束账期的每月，格式：yyyyMM:"+getLastMonthFromPerMonth4("202206","202209"));
			System.out.println("计算两个时间的天数差："+mathdays("2022-01-31 19:00:00".substring(0,10),"2022-01-01","yyyy-MM-dd"));


		}catch (Exception e){
			e.printStackTrace();
		}


	}
}