package com.uas.platform.b2b.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

	public static final int COMPARE_NONE = 0;
	public static final int COMPARE_SECOND = 1;
	public static final int COMPARE_MINUTE = 2;
	public static final int COMPARE_HOUR = 3;
	public static final int COMPARE_DAY = 4;
	static final SimpleDateFormat ym = new SimpleDateFormat("yyyyMM");
	static final SimpleDateFormat YM = new SimpleDateFormat("yyyy-MM");
	static final SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
	static final SimpleDateFormat YMD_HMS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取月份起始日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMinMonthDate(Date date) {
		date = date == null ? new Date() : date;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	/**
	 * 获取月份起始日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getMinMonthDateS(Date date) {
		return YMD.format(getMinMonthDate(date));
	}

	/**
	 * 获取月份最后日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMaxMonthDate(Date date) {
		date = date == null ? new Date() : date;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	/**
	 * 获取月份最后日期
	 * 
	 * @param date
	 * @return
	 */
	public static String getMaxMonthDateS(Date date) {
		return YMD.format(getMaxMonthDate(date));
	}

	/**
	 * 比较日期大小
	 * 
	 * @param dateParam1
	 * @param dateParam2
	 * @param compareType
	 *            精确位(天、小时、分钟、秒)
	 * @return 1表示dateParam1大于dateParam2,0表示等于,-1表示小于
	 */
	public static int compare(Date dateParam1, Date dateParam2, int compareType) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(dateParam1);
		calendar1.clear(Calendar.MILLISECOND);
		if (compareType >= COMPARE_SECOND) {
            calendar1.clear(Calendar.SECOND);
        }
		if (compareType >= COMPARE_MINUTE) {
            calendar1.clear(Calendar.MINUTE);
        }
		if (compareType >= COMPARE_HOUR) {
			calendar1.clear(Calendar.HOUR_OF_DAY);
			calendar1.clear(Calendar.HOUR);
			calendar1.clear(Calendar.AM_PM);
		}
		if (compareType >= COMPARE_DAY) {
            calendar1.clear(Calendar.DAY_OF_MONTH);
        }
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(dateParam2);
		calendar2.clear(Calendar.MILLISECOND);
		if (compareType >= COMPARE_SECOND) {
            calendar2.clear(Calendar.SECOND);
        }
		if (compareType >= COMPARE_MINUTE) {
            calendar2.clear(Calendar.MINUTE);
        }
		if (compareType >= COMPARE_HOUR) {
			calendar2.clear(Calendar.HOUR_OF_DAY);
			calendar2.clear(Calendar.HOUR);
			calendar2.clear(Calendar.AM_PM);
		}
		if (compareType >= COMPARE_DAY) {
            calendar2.clear(Calendar.DAY_OF_MONTH);
        }
		return calendar1.compareTo(calendar2);
	}

	/**
	 * 截取指定日期的年月
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getYearmonth(Date date) {
		if (date == null) {
			date = new Date();
		}
		return Integer.parseInt(ym.format(date));
	}

	/**
	 * 截取指定日期的年月
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getYearmonth(String date) {
		return Integer.parseInt(ym.format(parse(date, null)));
	}

	public static Date parse(String date, String f) {
		if (date == null || "".equals(date)) {
			return new Date();
		}
		if (f == null || "".equals(f)) {
			try {
				return date.contains(" ") ? YMD_HMS.parse(date) : YMD.parse(date);
			} catch (ParseException e) {
				return new Date();
			}
		}
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return new Date();
		}
	}

	/**
	 * 当前时间
	 * 
	 * @return <h3>String</h3>
	 */
	public static String getCurrentDate() {
		return format(new Date(), null);
	}

	public static String format(Date date, String f) {
		if (date == null) {
			date = new Date();
		}
		if (f == null) {
			return YMD.format(date);
		}
		SimpleDateFormat sdf = new SimpleDateFormat(f);
		return sdf.format(date);
	}

	/**
	 * 月份加减
	 * 
	 * @param date
	 * @param increase
	 *            加减月份数，负数表示减
	 * @return
	 */
	public static Integer addMonth(Date date, int increase) {
		if (date == null) {
            date = new Date();
        }
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, increase);
		return getYearmonth(calendar.getTime());
	}

	/**
	 * 月份加减
	 * 
	 * @param date
	 * @param increase
	 *            加减月份数，负数表示减
	 * @return
	 */
	public static Integer addMonth(String date, int increase) {
		return addMonth(parse(date, null), increase);
	}
}
