/*
 * DateTimeUtils.java created on 2012-5-11 下午1:32:48 by xie-qing
 */

package com.dgt.graphdemo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

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


/**
 * 日期时间工具类
 * @author: xie-qing
 * @since: 2012-5-11
 */
@Slf4j
public class DateTimeUtils {


	public final static String SDF_YYYY = "yyyy";

	public final static String SDF_YYYYMM = "yyyy-MM";

	public final static String SDF_YYYYMMDD = "yyyy-MM-dd";

	public final static String SDF_YYYYMMDD_HH = "yyyy-MM-dd HH";

	public final static String SDF_YYYYMMDD_HHMM = "yyyy-MM-dd HH:mm";

	public final static String SDF_YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";

	public final static String SDF_MM = "MM";

	public final static String SDF_DD = "dd";

	public final static String SDF_HH = "HH";

	public final static String SDFMM = "mm";

	public final static String SDF_SS = "ss";

	public final static String SDF_HHMM = "HH:mm";

	public final static String SDF_HHMMSS = "HH:mm:ss";
	
	public final static int MAX_NUMBER_FOR_DAY=30;

	/**
	 * 是否是同一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
//	public static boolean isSameDate(Date date1, Date date2) {
//		if (date1!=null&&date2!=null){
//
//			return toDayStart(date1).getTime() == toDayStart(date2).getTime();
//		}
//		return false;
//	}

	/**
	 * 比较date1是否在date2之前
	 * @creator: xie-qing
	 * @createDate: 2012-8-20 
	 * @modifier:
	 * @modifiedDate:
	 * @param date1
	 * @param date2
	 * @return
	 */
//	public static boolean isBefore(Date date1, Date date2) {
//		if (date1!=null&&date2!=null) {
//			return toDayStart(date1).getTime() <= toDayStart(date2).getTime();
//		}
//		return false;
//	}

	/**
	 * 日期转换成字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		return DateFormatUtils.format(date, pattern);
	}

	/**
	 * 字符串转换成日期
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date stringToDate(String date, String pattern) {
		try {
			return DateUtils.parseDate(date, new String[] {
				pattern
			});
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);

			return null;
		}
	}

	/**
	 * 指定日期的开始时间: 2010-10-10 00:00:00
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDayStart(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYYMMDD);
		try {
			return sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);

			return null;
		} catch (Exception e) {
			log.error("发生错误，错误信息为："+e);
			return null;
		}
	}

	/**
	 * 指定日期的最后时间: 2010-10-10 23:59:59
	 * 
	 * @param date
	 * @return
	 */
	public static Date toDayEnd(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYYMMDD);
		Date tmp = null;
		try {
			tmp = sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);
			return null;
		}
		return new Date(tmp.getTime() + 24 * 60 * 60 * 1000 - 1);
	}

	/**
	 * 获取当前日期的年份
	 * 
	 * @return
	 */
	public static int getYear() {
		return getYear(new Date());
	}

	/**
	 * 获取指定日期的年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		String year = DateFormatUtils.format(date, SDF_YYYY);
		return Integer.parseInt(year);

	}

	/**
	 * 获取当前日期的月份
	 * 
	 * @return
	 */
	public static int getMonth() {
		return getMonth(new Date());
	}

	/**
	 * 获取指定日期的月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		String month = DateFormatUtils.format(date, SDF_MM);
		return Integer.parseInt(month);
	}

	/**
	 * 获取当前日期几号
	 * 
	 * @return
	 */
	public static int getDay() {
		return getDay(new Date());
	}

	/**
	 * 获取指定日期几号
	 * 
	 * @param date
	 * @return
	 */
	public static int getDay(Date date) {
		String day = DateFormatUtils.format(date, SDF_DD);
		return Integer.parseInt(day);
	}

	/**
	 * 获取当前日期几点
	 * 
	 * @return
	 */
	public static int getHour() {
		return getHour(new Date());
	}

	/**
	 * 获取指定日期几点
	 * 
	 * @return
	 */
	public static int getHour(Date date) {
		String hours = DateFormatUtils.format(date, SDF_HH);
		return Integer.parseInt(hours);
	}

	/**
	 * 获取当前日期几分钟
	 * 
	 * @return
	 */
	public static int getMinute() {
		return getMinute(new Date());
	}

	/**
	 * 获取指定日期几分钟
	 * 
	 * @return
	 */
	public static int getMinute(Date date) {
		String minutes = DateFormatUtils.format(date, SDFMM);
		return Integer.parseInt(minutes);
	}

	/**
	 * 获取当前日期几秒钟
	 * 
	 * @return
	 */
	public static int getSecond() {
		return getSecond(new Date());
	}

	/**
	 * 获取指定日期几秒钟
	 * 
	 * @return
	 */
	public static int getSecond(Date date) {
		String seconds = DateFormatUtils.format(date, SDF_SS);
		return Integer.parseInt(seconds);
	}

	/**
	 * 获取当前日期是星期几
	 * 
	 * @return
	 */
	public static int getWeek() {
		return getWeek(new Date());
	}

	/**
	 * 获取指定日期是星期几
	 * 
	 * @return
	 */
	public static int getWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
		return week < 0 ? 0 : week;
	}

	/**
	 * 获取当前月有多少天
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonthDays(Date date) {
		if (date == null) {
			throw new RuntimeException("date is null");
		}
		int year = getYear(date);
		int month = getMonth(date);
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
			return 31;
		} else if (month == 2) {
			if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
				return 29;
			} else {
				return 28;
			}
		} else {
			return 30;
		}
	}

	/**
	 * 获取两个日期之间相差几年(2011-10-10至2013-10-09 算1年, 2011-10-10至2013-10-10 算2年)
	 * 
	 * @param date1  开始日期
	 * @param date2  结束日期
	 * @return
	 */
	public static int getDiffYears(Date date1, Date date2) {
		//return Math.abs(getYear(date2) - getYear(date1));
		int y = getYear(date2), m = getMonth(date2), d = getDay(date2), yFrom = getYear(date1), mFrom = getMonth(date1), dFrom = getDay(date1);
		// 1. 当前的月份大于出生月份
		if (m > mFrom) {
			return y - yFrom;
		}
		// 2. 当前的月份等于出生月份
		else if (m == mFrom) {
			if (d >= dFrom) {
				return y - yFrom;
			} else {
				return y - yFrom - 1;
			}
		}
		// 3. 当前的月份小于出生月份
		else if (m < mFrom) {
			return y - yFrom - 1;
		}
		else {
			return 0;
		}
	}

	/**
	 * 获取两个日期之间相差几月
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int getDiffMonths(Date date1, Date date2) {
		return Math.abs(getYear(date2) * 12 + getMonth(date2) - getYear(date1) * 12 - getMonth(date1));
	}

	/**
	 * 获取两个日期之间相差几天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
/*	public static int getDiffDays(Date date1, Date date2) {
		if (date1!=null&&date2!=null){

			long diffTimes = Math.abs(toDayStart(date2).getTime() - toDayStart(date1).getTime());
			return (int) (diffTimes / (1000 * 60 * 60 * 24));
		}

	}*/

	/**
	 * 获取当前月份的第一天
	 * 
	 * @return
	 */
	public static Date getFirstDayOfCurrentMonth() {
		return getFirstDayOfCurrentMonth(new Date());
	}

	/**
	 * 获取指定月份的第一天
	 * 
	 * @return
	 */
	public static Date getFirstDayOfCurrentMonth(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYYMM);
		try {
			return sdf.parse(sdf.format(date));
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);
			return null;
		}
	}

	/**
	 * 获取当前月份的最后一天
	 * 
	 * @return
	 */
	public static Date getLastDayOfCurrentMonth() {
		return getLastDayOfCurrentMonth(new Date());
	}

	/**
	 * 获取指定月份的最后一天
	 * 
	 * @return
	 */
	public static Date getLastDayOfCurrentMonth(Date date) {
		Date temp = DateUtils.addMonths(date, 1);
		SimpleDateFormat sdf = new SimpleDateFormat(SDF_YYYYMM);
		try {
			Long time = sdf.parse(sdf.format(temp)).getTime();
			return new Date(time - 1);
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);
			return null;
		}
	}

	/**
	 * 获取当前星期的第一天(周一)
	 * 
	 * @return
	 */
	public static Date getFirstDayOfCurrentWeek() {
		return getFirstDayOfCurrentWeek(new Date());
	}

	/**
	 * 获取指定星期的第一天(周一)
	 * 
	 * @return
	 */
	public static Date getFirstDayOfCurrentWeek(Date date) {
		int week = getWeek(date);
		//星期天
		if (0 == week) {
			return DateUtils.addDays(date, -6);
		} else {
			return DateUtils.addDays(date, 1 - week);
		}
	}

	/**
	 * 获取当前星期的最后一天(周日)
	 * 
	 * @return
	 */
	public static Date getLastDayOfCurrentWeek() {
		return getLastDayOfCurrentWeek(new Date());
	}

	/**
	 * 获取指定星期的最后一天(周日)
	 * 
	 * @return
	 */
	public static Date getLastDayOfCurrentWeek(Date date) {
		int week = getWeek(date);
		//星期天
		if (0 == week) {
			return date;
		} else {
			return DateUtils.addDays(date, 7 - week);
		}
	}

	/**
	 * 根据出生日期获取年龄
	 * 
	 * @param birthday
	 * @return
	 */
	public static int getAge(Date birthday) {
		int y = getYear(), m = getMonth(), d = getDay(), yFrom = getYear(birthday), mFrom = getMonth(birthday), dFrom = getDay(birthday);
		// 1. 当前的月份大于出生月份
		if (m > mFrom) {
			return y - yFrom;
		}
		// 2. 当前的月份等于出生月份
		else if (m == mFrom) {
			if (d >= dFrom) {
				return y - yFrom;
			} else {
				return y - yFrom - 1;
			}
		}
		// 3. 当前的月份小于出生月份
		else if (m < mFrom) {
			return y - yFrom - 1;
		}
		else{
			return 0;
		}
		
	}

	public static void main(String[] args) {
		//		System.out.println(getWeek(stringToDate("2011-08-06", "yyyy-MM-dd")));
		//		System.out.println(dateToString(getFirstDayOfCurrentWeek(new Date()), "yyyy-MM-dd"));
		//		System.out.println(dateToString(getLastDayOfCurrentWeek(new Date()), "yyyy-MM-dd"));

		Date date = DateTimeUtils.stringToDate("2011-06-07", "yyyy-MM-dd");
		int n = DateTimeUtils.getDiffMonths(new Date(), date);
		//System.out.println(n);
		//int m = DateTimeUtils.getDiffYears(new Date(), date);
		int m = DateTimeUtils.getDiffYears(date, new Date());
		//System.out.println(m);

		//		System.out.println(getAge(DateTimeUtils.stringToDate("2000-12-30", "yyyy-MM-dd")));
		//		
		//		int k = 10;
		//		System.out.println(k/3);
	}

	/**
	 * @creator: jiminghu
	 * @createDate: 2017年5月31日 
	 * @modifier:
	 * @modifiedDate:
	 * @param orDefault
	 * @return
	 */
	public static Date standarTime(String orDefault) {
		try {
			return DateUtils.parseDate(orDefault, DateTimeUtils.SDF_YYYYMMDD_HHMMSS, DateTimeUtils.SDF_YYYYMMDD_HHMM , DateTimeUtils.SDF_YYYYMMDD_HH, DateTimeUtils.SDF_YYYYMMDD, DateTimeUtils.SDF_YYYYMM);
		} catch (ParseException e) {
			log.error("发生错误，错误信息为："+e);
			return null;
		}
	}
}
