/**
 * @包名称 com.sagacloud.common
 * @文件名 DateUtil.java
 * @创建者 wanghailong
 * @邮箱 wanghailong@persagy.com  
 * @修改描述 
 */

package com.persagy.util;

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

/** 
 * 功能描述： 日期工具类
 * @类型名称 DateUtil
 * @创建者 wanghailong
 * @邮箱 wanghailong@persagy.com  
 * @修改描述 
 */
public class DateUtil {
	public static final String sdfTime = "yyyyMMddHHmmss";
	public static final String sdftime = "yyyyMMddHHmmss";
	public static final String sdfTimeMinute = "yyyyMMddHHmm";
	public static final String sdftimemilli = "yyyyMMddHHmmssSSS";
	public static final String sdfDay = "yyyyMMdd";
	public static final String sdf_time = "yyyy-MM-dd HH:mm:ss";
	public static final String date = "yyyy-MM-dd";
	public static final String time = "HH:mm:ss";
	public static final String customStateTimeFormat = "yyyyMMddHHmm";


    public static String getNowTimeStr() {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
    public static String getNowTimeStrSSS() {
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss.SSS");
    	return sdf.format(new Date());
    }

    public static String getNowDate() {
    	SimpleDateFormat sdf = new SimpleDateFormat(sdfDay);
    	return sdf.format(new Date());
    }

	/**
	 * 获取当前的UTC时间,精确到毫秒
	 */
	public static Long getUtcTimeNow(){
		Date dateNow = new Date();
		Long lRes = dateNow.getTime();
		return lRes;
	}

	/**
	 * 转换时间格式
	 * @param dateStr
	 * @param fromDateFormat
	 * @param toDateFormat
	 * @return
	 * @throws ParseException
	 */
	public static String transferDateFormat(String dateStr, String fromDateFormat, String toDateFormat) throws ParseException {
		SimpleDateFormat fromSdf = new SimpleDateFormat(fromDateFormat);
		SimpleDateFormat toSdf = new SimpleDateFormat(toDateFormat);
		return toSdf.format(fromSdf.parse(dateStr));
	}

	/**
	 * 获取时间小时数
	 * @return
	 */
	public static String getNowTimeMinutes(){
		Date date = new Date();
		long time = date.getTime();
		return (time/100) + "";
	}

	public static String getTimeMinutes(String timeStr, String timeFormat){
		try {
		SimpleDateFormat simpleDateFormat  = new SimpleDateFormat(timeFormat);
		Date date;
			date = simpleDateFormat.parse(timeStr);
			long time = date.getTime();
			return (time/100) + "";
		} catch (ParseException e) {
		}
		return null;
	}

	/**
	 * 获取时间 yyyyMMdd 转换成int
	 * @return
	 */
	public static String getDayFromDateString(String dateStr, String timeFormat) throws ParseException{
		Date date = parseDate(timeFormat, dateStr);
		return formatStr(sdfDay, date);
	}

	/**
	 *
	 * @param timeStr
	 * @param timeFormat
	 * @param aheadHour
	 * @return
	 * @throws Exception
	 */
	public static Calendar getBeforeTime(String timeStr, String timeFormat, int aheadHour) throws Exception{
		Calendar calendar = Calendar.getInstance();
		Date date = parseDate(timeFormat, timeStr);
		calendar.setTime(date);
		if(aheadHour != 0){
			calendar.add(Calendar.HOUR, aheadHour * -1);
		}
		return calendar;
	}

	/**
	 * 根据日历获取时间yyyyMMdd
	 * @param calendar
	 * @return
	 */
	public static String getDayFromCalendar(Calendar calendar){
		Date date = calendar.getTime();
		return formatStr(sdfDay, date);
	}

	/**
	 *
	 * @param timeStr
	 * @param timeFormat
	 * @param aheadHour
	 * @return
	 * @throws Exception
	 */
	public static String getAfterDateOfDay(String timeStr, String timeFormat, int aheadHour){
		Calendar calendar = Calendar.getInstance();
		Date date = new Date();
		try {
			date = parseDate(timeFormat, timeStr);
		} catch (ParseException e) {
		}
		calendar.setTime(date);
		if(aheadHour != 0){
			calendar.add(Calendar.DATE, aheadHour);
		}
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取m个天后的日期
	 * @param afterDay
	 * @return yyyyMMdd
	 */
	public static String getAfterDateOfDay(int afterDay){
		Calendar calendar = Calendar.getInstance();
		if(afterDay !=0){
			calendar.add(Calendar.DATE, afterDay);
		}
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取m个小时后的日期
	 * @param afterHour
	 * @return
	 */
	public static String getAfterDateOfHour(int afterHour){
		Calendar calendar = Calendar.getInstance();
		if(afterHour !=0){
			calendar.add(Calendar.HOUR, afterHour);
		}
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取n天、m个小时后的日期
	 * @param afterHour
	 * @return
	 */
	public static String getAfterDate(int afterYear, int afterMonth, int afterDay, int afterHour){
		Calendar calendar = Calendar.getInstance();
		if(afterYear !=0){
			calendar.add(Calendar.YEAR, afterYear);
		}
		if(afterMonth !=0){
			calendar.add(Calendar.MONTH, afterMonth);
		}
		if(afterDay !=0){
			calendar.add(Calendar.DATE, afterDay);
		}
		if(afterHour !=0){
			calendar.add(Calendar.HOUR, afterHour);
		}
		return new SimpleDateFormat("yyyyMMdd").format(calendar.getTime());
	}

	/**
	 * 获取n天、m个小时后的日期
	 * @param afterHour
	 * @return
	 */
	public static String getAfterDate(Date date, String timeFormat, int afterYear, int afterMonth, int afterDay, int afterHour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if(afterYear !=0){
			calendar.add(Calendar.YEAR, afterYear);
		}
		if(afterMonth !=0){
			calendar.add(Calendar.MONTH, afterMonth);
		}
		if(afterDay !=0){
			calendar.add(Calendar.DATE, afterDay);
		}
		if(afterHour !=0){
			calendar.add(Calendar.HOUR, afterHour);
		}
		return new SimpleDateFormat(timeFormat).format(calendar.getTime());
	}

	/**
	 * 获取时间差值-格式**天**小时**分钟
	 * @param fromTime
	 * @param toTime
	 * @param timeFormat
	 * @return
	 * @throws ParseException
	 */
	public static String getTimeDiffence(String fromTime, String toTime, String timeFormat) throws ParseException {
		if(fromTime == null || toTime == null)
			return null;
		SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
		Date startTime = sdf.parse(fromTime);
		Date endTime = sdf.parse(toTime);
		long diffenceValue = endTime.getTime() - startTime.getTime();
		long days = diffenceValue / (24*60*60*1000);
		long hours = (diffenceValue/(60*60*1000)-days*24);
		long min=((diffenceValue/(60*1000))-days*24*60-hours*60);
		String str = "";
		if(days != 0){
			str += days + "天";
		}
		if(hours != 0 || days != 0){
			str += hours + "小时";
		}
		if(min != 0 || days != 0 || hours != 0 || (days == 0 && hours == 0)){
			str += min + "分钟";
		}
		return str;
	}

	public static String formatStr(String pattern, Date date) {
		String str = new SimpleDateFormat(pattern).format(date);
		return str;
	}
	public static Date parseDate(String pattern, String str) throws ParseException {
		Date date = new SimpleDateFormat(pattern).parse(str);
		return date;
	}

	/**
	* 将毫秒转成时间格式的字符串
	* @param pattern
	* @param milis
	* @return
	*/
	public static String parseDate(String pattern, long milis){
	String date = new SimpleDateFormat(pattern).format(milis);
	return date;
	}

	/**
	 * 当前时间到第二日凌晨的秒数
	 */
    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

	public static void main(String[] args) {
		System.out.println("AboLuo".substring(0,1));
	}
}
