package com.saye.hospitalgd.commons.date;

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

public class DateDUtil {
	
	
	public static String yyyy_MM_dd = "yyyy-MM-dd";
	public static String yyyyMMdd = "yyyyMMdd";
	public static String yyyyMM = "yyyyMM";
	public static String yyyy_MM = "yyyy-MM";
	public static String yyyy_MM_dd_HH_00 = "yyyy-MM-dd HH:00";
	public static String yyyy_MM_dd_HH_mm = "yyyy-MM-dd HH:mm";
	public static String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static String yyyy_MM_dd_HH_mm_ss_SS = "yyyy-MM-dd HH:mm:ss.SS";
	public static String yyyyMMddHHmm = "yyyyMMddHHmm";
	public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static String yyyyMMddHHmmssSS = "yyyyMMddHHmmssSS";
	public static String yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";
	public static String yyMMdd = "yyMMdd";
	public static String yyyy_MM_dd_00_00 = "yyyy-MM-dd 00:00";
	public static String yyyyMMddHH_mm_ss = "yyyyMMddHH:mm:ss";

	/**
	 * 将字符串时间改成Date类型
	 * @param format
	 * @param dateStr
	 * @return
	 */
	public  static Date strToDate(String format,String dateStr) {
		
		Date date = null;
		
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
			date = simpleDateFormat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return date;
	}
	
	
	/**
	 * 将Date时间转成字符串
	 * @param format
	 * @param date
	 * @return
	 */
	public static String DateToStr(String format,Date date){
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		
		return simpleDateFormat.format(date);	
	}
	
	
	
	/**
	* 获取2个字符日期的天数差
	* @param p_startDate
	* @param p_endDate
	* @return 天数差
	*/
	public static long getDaysOfTowDiffDate( String p_startDate, String p_endDate ){
	  
		Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, p_startDate);
		Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd, p_endDate);
		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenDays = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60 * 60 * 24 ) );
		return betweenDays;
	}
	
	
	/**
	* 获取2个字符日期的天数差
	* @param l_startDate
	* @param l_endDate
	* @return 天数差
	*/
	public static long getDaysOfTowDiffDate( Date l_startDate, Date l_endDate ){
	  
		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenDays = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60 * 60 * 24 ) );
		return betweenDays;
	}
	
	
	/**
	 * 给出日期添加一段时间后的日期
	 * @param dateStr
	 * @param plus
	 * @return
	 */
	public static String getPlusDays(String format,String dateStr,long plus){
		
		Date date = DateDUtil.strToDate(format, dateStr);
		
		long time = date.getTime()+ plus*24*60*60*1000;
		
		
		return DateDUtil.DateToStr(format,new Date(time));
	}
	
	
	/**
	 * 给出日期添加一段时间后的日期
	 * @param format
	 * @param date
	 * @param plus
	 * @return
	 */
	public static String getPlusDays(String format,Date date,long plus){
		
		
		long time = date.getTime()+ plus*24*60*60*1000;
		
		
		return DateDUtil.DateToStr(format,new Date(time));
	}
	
	/**
	 * 给出日期添加一段时间前的日期
	 * @param format
	 * @param date
	 * @param forth
	 * @return
	 */
	public static String getForthDays(String format,Date date,long forth){
		
		
		long time = date.getTime()- forth*24*60*60*1000;
		
		
		return DateDUtil.DateToStr(format,new Date(time));
	}
	
	/**
	 * 给出时间添加几个小时后的时间
	 * @param format
	 * @param dateStr
	 * @param plus
	 * @return
	 */
	public static String getPlusHours(String format,String dateStr,long plus){
		
		Date date = DateDUtil.strToDate(format, dateStr);
		
		long time = date.getTime()+ plus*60*60*1000;
		
		
		return DateDUtil.DateToStr(format,new Date(time));
	}
	
	/**
	 * 给出时间添加几个分钟后的时间
	 * @param format
	 * @param dateStr
	 * @param plus
	 * @return
	 */
	public static String getPlusMinutes(String format,String dateStr,long plus){
		
		Date date = DateDUtil.strToDate(format, dateStr);
		
		long time = date.getTime()+ plus*60*1000;
		
		
		return DateDUtil.DateToStr(format,new Date(time));
	}
	
	/**
	 * 得到当前时间，格式如：yyyy-MM-dd HH:mm:ss:SS
	 * @return
	 */
	public static String getCurrentTime(){
		
		String nowTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, new Date());
		return nowTime;
	}
	
	/**
	 * 得到当前时间，格式如：yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getTheCurrentTime(){
		
		String nowTime = DateDUtil.DateToStr(DateDUtil.yyyy_MM_dd_HH_mm_ss, new Date());
		return nowTime;
	}
	
	/**
	 * 得到当前日期，格式如：yyyyMMdd
	 * @return
	 */
	public static String getCurrentDate(){
		
		String nowDate = DateDUtil.DateToStr(DateDUtil.yyyyMMdd, new Date());		
		return nowDate;
	}
	
	/**
	 * 得到当前日期，格式如：yyyyMMdd
	 * @return
	 */
	public static String getCurrentDate(String format){
		
		String nowDate = DateDUtil.DateToStr(format, new Date());		
		return nowDate;
	}
	
	/**
	 * 获取2个字符日期的分钟数差
	 * @param p_startDate
	 * @param p_endDate
	 * @return 相差的分钟
	 */
	public static long getMinutesOfTowDiffDate(String p_startDate, String p_endDate ){
	  
		Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, p_startDate);
		Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss_SS, p_endDate);
		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenMinutes = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60) );
		return betweenMinutes;
	}
	
	/**
	 * 获取2个字符日期的分钟数差
	 * @param p_startDate
	 * @param p_endDate
	 * @return 相差的分钟
	 */
	public static long getMinutesOfTowDiffDateMin(String format,String p_startDate, String p_endDate ){
	  
		Date l_startDate = DateDUtil.strToDate(format, p_startDate);
		Date l_endDate = DateDUtil.strToDate(format, p_endDate);
		long l_startTime = l_startDate.getTime();
		long l_endTime = l_endDate.getTime();
		long betweenMinutes = (long) ( ( l_endTime - l_startTime ) / ( 1000 * 60) );
		return betweenMinutes;
	}
	public static long getMonthIntervalOfTowDiffDate(String p_startMonth, String p_endMonth){
		
		Date l_startDate = DateDUtil.strToDate(DateDUtil.yyyy_MM, p_startMonth);
		Date l_endDate = DateDUtil.strToDate(DateDUtil.yyyy_MM, p_endMonth);


		Calendar calender = Calendar.getInstance();
		calender.setTime(l_startDate);

		long l_startMonth = calender.get(Calendar.MONTH)+1;
		long l_startYear = calender.get(Calendar.YEAR);

		calender.setTime(l_endDate);

		long l_endMonth =calender.get(Calendar.MONTH)+1;
		long l_endYear = calender.get(Calendar.YEAR);
		
		long betweenYear = l_endYear  - l_startYear;
		long betweenMonth = (long)  ( l_endMonth - l_startMonth ) ;
		
		return betweenYear * 12 + betweenMonth;
	}
	
	/**
	 * 将字符串日期转为cron表达式
	 */
	public static String getCron(String execute_time) throws Exception{ 
		
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
			Date date = simpleDateFormat.parse(execute_time);
			SimpleDateFormat dateFormat = new SimpleDateFormat("ss mm HH dd MM ? yyyy"); 
			return dateFormat.format(date);
    } 
	/**
	 * 获取当前年月日日期
	 */

	public static String getCDate(){
		 Calendar now = Calendar.getInstance(); 
		 String year=now.get(Calendar.YEAR)+"";
		 String month=(now.get(Calendar.MONTH) + 1) + "";
		 String day=now.get(Calendar.DAY_OF_MONTH)+"";
		 if ((now.get(Calendar.MONTH) + 1) < 10) month = "0" + month;
	     if (now.get(Calendar.DAY_OF_MONTH) < 10) day= "0" + day;
		String nowDate = year+"年"+month+"月"+day+"日";
		return nowDate;
	}

	/**
	 * 获取两个日期字符串之间的日期集合
	 * @param startTime:String
	 * @param endTime:String
	 * @return list:yyyy-MM-dd
	 */
	public static List<String> getBetweenDate(String startTime, String endTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 声明保存日期集合
		List<String> list = new ArrayList<String>();
		try {
			// 转化成日期类型
			Date startDate = sdf.parse(startTime);
			Date endDate = sdf.parse(endTime);

			//用Calendar 进行日期比较判断
			Calendar calendar = Calendar.getInstance();
			while (startDate.getTime()<=endDate.getTime()){
				// 把日期添加到集合
				list.add(sdf.format(startDate));
				// 设置日期
				calendar.setTime(startDate);
				//把日期增加一天
				calendar.add(Calendar.DATE, 1);
				// 获取增加后的日期
				startDate=calendar.getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}

}
