package com.iamberry.wechat.tools;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 
 * @author 何秀刚
 * Class Description:日期转换工具类
 * Create Date:2016年5月5日
 * Update Date:2016年5月5日
 */
public class DateTimeUtil {
	public static String defaultSimpleFormater = "yyyy-MM-dd HH:mm:ss";

	/**
	 * @param day 天数（如果day数为负数,说明是此日期前的天数）
	 * @param day
	 * @return
	 */
	public static Calendar getRecentDay(int day){
		Date date=new Date();
		Calendar c = Calendar.getInstance();
	    c.setTime(date);
	    c.add(Calendar.DAY_OF_YEAR, day);
	    return c;
	}
	
	/**
	 * mon 天数（如果mon数为负数,说明是此日期前的天数）
	 * @param mon
	 * @return
	 */
	public static Calendar getLastMon(int mon){
		Date date=new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, mon);
		return cal;
	}

    public static String yyyyMMDDFormat(){
        SimpleDateFormat sdf = new SimpleDateFormat(NameUtils.DATE_FORMAT);
        return sdf.format(new Date());
    }

    public static String timeFormat(){
        SimpleDateFormat sdf = new SimpleDateFormat(NameUtils.TIME_FORMAT);
        return sdf.format(new Date());
    }

    /**
     * 获取一天的开始时间   先转换成 yyyy-MM-dd 这样的字串就只保留了天数，再转成long 类型的时间戳
     *  本来想 time / (24*3600*1000) *24*3600*1000 这样就能把一天的时间里的多的那部分去掉，但是不知道为什么通过这样的方式总是取出来的时间是 当天的八点，如果在计算后的时间减掉
     *  八小时 时间改变之后却又无法控制，暂时没有找到比较好的方法.
     */
    public static long  getDayStartTime(long time) throws ParseException{
        Date date = new Date(time);
        String fmt = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        return sdf.parse(sdf.format(date)).getTime();
    }

    public static Long getNowTime() {
        Calendar todayStart = Calendar.getInstance();
        return todayStart.getTime().getTime();
    }

    public static boolean isToday(Date date){
        String dateStr = format(date,"yyyy-MM-dd");
        String today = format(new Date(), "yyyy-MM-dd");

        return today.equals(dateStr);
    }

    public static Date getDayBegin() throws ParseException {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return new Date(todayStart.getTime().getTime());
    }

    public static Date getDayEnd() throws ParseException {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        return new Date(todayStart.getTime().getTime());
    }

    public static Long getDayBegin(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(NameUtils.DATE_FORMAT);
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(sdf.parse(date));
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Long getDayEnd(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(NameUtils.DATE_FORMAT);
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(sdf.parse(date));
        todayStart.set(Calendar.HOUR_OF_DAY, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        return todayStart.getTime().getTime();
    }

    public static Long getDayBegin(Date date) throws ParseException {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Date getDayBeginDate(Date date) throws ParseException {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime();
    }

    public static int between(Date beginDate,Date endDate){
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();
        long value = (endTime - beginTime) / (1000 * 60 * 60 * 24) + 1;
        return (int)value;
    }

    public static Long getDayEnd(Date date) throws ParseException {
        Calendar todayStart = Calendar.getInstance();
        todayStart.setTime(date);
        todayStart.set(Calendar.HOUR_OF_DAY, 23);
        todayStart.set(Calendar.MINUTE, 59);
        todayStart.set(Calendar.SECOND, 59);
        return todayStart.getTime().getTime();
    }

    public static Long getTodayBegin(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Long getTodayEndTime(){
        Calendar yesEnd = Calendar.getInstance();
        yesEnd.set(Calendar.HOUR_OF_DAY, 23);
        yesEnd.set(Calendar.MINUTE, 59);
        yesEnd.set(Calendar.SECOND, 59);
        return yesEnd.getTime().getTime();
    }

    public static Long getYesterdayStartTime(){
        Calendar todayStart = Calendar.getInstance();
        todayStart.add(Calendar.DATE, -1);
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        return todayStart.getTime().getTime();
    }

    public static Long getYesterdayEndTime(){
        Calendar yesEnd = Calendar.getInstance();
        yesEnd.add(Calendar.DATE, -1);
        yesEnd.set(Calendar.HOUR_OF_DAY, 23);
        yesEnd.set(Calendar.MINUTE, 59);
        yesEnd.set(Calendar.SECOND, 59);
        return yesEnd.getTime().getTime();
    }

    public static int getMinute24(long time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);

        return hour*60+minutes;

    }

    /**
     * 把某种格式的字串时间转换成long类型
     * @param
     * @return
     * @throws ParseException
     */
    public static long   pasreStringToLong(String date,String fmt) throws ParseException{
        String fmtdefault = "yyyy-MM-dd HH:mm";
        SimpleDateFormat sdf = null;
        if(fmt==null || fmt.equals("")){
            sdf = new SimpleDateFormat(fmtdefault);
        }else{
            sdf = new SimpleDateFormat(fmt);
        }
        return sdf.parse(date).getTime();
    }

    /**
     * 默认简单日期字符串
     */
    public static String getDefaultSimpleFormater() {
        return defaultSimpleFormater;
    }

    /**
     * 设置默认简单日期格式字符串
     * @param defaultFormatString
     */
    public static void setDefaultSimpleFormater(String defaultFormatString) {
        DateTimeUtil.defaultSimpleFormater = defaultFormatString;
    }

    /**
     * 格式化日期
     *
     * @param date
     * @param formatString
     * @return
     */
    public static String format(Date date, String formatString) {
        SimpleDateFormat df = new SimpleDateFormat(formatString);
        return df.format(date);
    }

    /**
     * 格式化日期(使用默认格式)
     *
     * @param date
     * @return
     */
    public static String format(Date date) {
        return format(date, defaultSimpleFormater);
    }

    /**
     * 转换成日期
     *
     * @param dateString
     * @param formatString
     * @return
     */
    public static Date parse(String dateString, String formatString) {
        SimpleDateFormat df = new SimpleDateFormat(formatString);
        try {
            return df.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 转换成日期(使用默认格式)
     *
     * @param dateString
     * @return
     */
    public static Date parse(String dateString) {
        return parse(dateString, defaultSimpleFormater);
    }

    /**
     * 昨天
     *
     * @return
     */
    public static Date yesterday() {
        return addDay(-1);
    }

    /**
     * 明天
     *
     * @return
     */
    public static Date tomorrow() {
        return addDay(1);
    }

    /**
     * 现在
     *
     * @return
     */
    public static Date now() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * 按日加
     *
     * @param value
     * @return
     */
    public static Date addDay(int value) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_YEAR, value);
        return now.getTime();
    }

    /**
     * 按日加,指定日期
     *
     * @param date
     * @param value
     * @return
     */
    public static Date addDay(Date date, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.DAY_OF_YEAR, value);
        return now.getTime();
    }

    /**
     * 按月加
     *
     * @param value
     * @return
     */
    public static Date addMonth(int value) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MONTH, value);
        return now.getTime();
    }

    /**
     * 按月加,指定日期
     *
     * @param date
     * @param value
     * @return
     */
    public static Date addMonth(Date date, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MONTH, value);
        return now.getTime();
    }

    /**
     * 按年加
     *
     * @param value
     * @return
     */
    public static Date addYear(int value) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.YEAR, value);
        return now.getTime();
    }

    /**
     * 按年加,指定日期
     *
     * @param date
     * @param value
     * @return
     */
    public static Date addYear(Date date, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.YEAR, value);
        return now.getTime();
    }

    /**
     * 按小时加
     *
     * @param value
     * @return
     */
    public static Date addHour(int value) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.HOUR_OF_DAY, value);
        return now.getTime();
    }

    /**
     * 按小时加,指定日期
     *
     * @param date
     * @param value
     * @return
     */
    public static Date addHour(Date date, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.HOUR_OF_DAY, value);
        return now.getTime();
    }

    /**
     * 按分钟加
     *
     * @param value
     * @return
     */
    public static Date addMinute(int value) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.MINUTE, value);
        return now.getTime();
    }

    /**
     * 按分钟加,指定日期
     *
     * @param date
     * @param value
     * @return
     */
    public static Date addMinute(Date date, int value) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.add(Calendar.MINUTE, value);
        return now.getTime();
    }

    /**
     * 年份
     *
     * @return
     */
    public static int year() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.YEAR);
    }

    /**
     * 月份
     *
     * @return
     */
    public static int month() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MONTH);
    }

    /**
     * 日(号)
     *
     * @return
     */
    public static int day() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 小时(点)
     *
     * @return
     */
    public static int hour() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.HOUR);
    }

    /**
     * 分钟
     *
     * @return
     */
    public static int minute() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.MINUTE);
    }

    /**
     * 秒
     *
     * @return
     */
    public static int second() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.SECOND);
    }

    /**
     * 星期几(礼拜几)
     *
     * @return
     */
    public static int weekday() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 是上午吗?
     *
     * @return
     */
    public static boolean isAm() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.AM_PM) == 0;
    }

    /**
     * 是下午吗?
     *
     * @return
     */
    public static boolean isPm() {
        Calendar now = Calendar.getInstance();
        return now.get(Calendar.AM_PM) == 1;
    }

    /**

     * @return 当前的时间戳 yyyy-MM-dd HH:mm:ss

     */
    public static String getNowUnix() {

        return dateTimeToUnix(new Date());

    }

    /**
     *  获取时间戳
     * @param date
     *      需要转化时间戳的时间
     * @return 时间戳
     */
    public static String dateTimeToUnix(Date date) {

        return Long.toString(date.getTime());

    }

    /**
     *  获取格式化时间字符串
     *
     * @return 格式化时间字符串
     */
    public static String formatNow() {
        return DateTimeUtil.format(DateTimeUtil.now());
    }

    /**
     *  获取格式化时间字符串
     * @return 格式化时间字符串
     */
    public static String formatToday() {
        return DateTimeUtil.format(DateTimeUtil.now(),NameUtils.DATE_FORMAT);
    }

    public static Timestamp getCurrentTimestamp() {
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        return currentTime;
    }

    /**
     *  将日期转换成当前月份1号的日期
     *
     * @return 当前月的1号所对应的日期
     */
    public static Date coverToHourDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return new Date(cal.getTimeInMillis());
    }

    /**
     *  将日期转换成当前月份1号的日期
     *
     * @return 当前月的1号所对应的日期
     */
    public static Date getFirstDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        // hour_of_day
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MONTH, date.getMonth());
//        cal.set(Calendar.MONTH, cal.getActualMinimum(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return new Date(cal.getTimeInMillis());
    }
    
    public static Date getLastDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
//        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.HOUR, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MONTH, date.getMonth());
//        cal.set(Calendar.MONTH, cal.getActualMaximum(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return new Date(cal.getTimeInMillis());
    }

    /**
     * 创建时间： 2013-6-17 上午10:42:59
     * 创建人：cutter.li
     * 参数： 把从db中取出来的sql的是时间转换成date的时间
     * 返回值： Date
     * 方法描述 :
     */
    public static Date transforFromSqlToUtilDate(java.sql.Date sqlDate) {

        if (null != sqlDate) {
            return new Date(sqlDate.getTime());
        }

        return null;
    }

    /**
     * 方法描述 :把应用系统的时间转换成sql的时间
     */
    public static java.sql.Date transforFromUtilToSqlDate(Date utilDate) {

        if (null != utilDate) {
            return new java.sql.Date(utilDate.getTime());
        }

        return null;
    }

    public static Date getLastWeekStart() {
        Calendar cal = Calendar.getInstance();
        int n = -2;
        cal.add(Calendar.DATE, n*7);
        cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        return cal.getTime();
    }

    public static Date getLastWeekEnd() {
        Calendar cal = Calendar.getInstance();
        int n = -1;
        cal.add(Calendar.DATE, n*7);
        cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        return cal.getTime();
    }
    
    /** 
     * 两个时间相差距离多少天多少小时多少分多少秒 
     * @param Date1
     * @param Date2
     * @return String 返回值为：xx天xx小时xx分xx秒 
     */  
    public static String getDistanceTime(Date one, Date two) {  
        long day = 0;  
        long hour = 0;  
        long min = 0;  
//        long sec = 0;  
        try {  
            long time1 = one.getTime();  
            long time2 = two.getTime();  
            long diff ;  
            if(time1<time2) {  
                diff = time2 - time1;  
            } else {  
                diff = time1 - time2;  
            }  
            day = diff / (24 * 60 * 60 * 1000);  
            hour = (diff / (60 * 60 * 1000) - day * 24);  
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  
           // sec = (diff/1000-day*24*60*60-hour*60*60-min*60);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //return day + "天" + hour + "小时" + min + "分" + sec + "秒";  
        return day + "天" + hour + "小时" + min + "分";
    }
    
    
    /** 
     * 两个时间相差距离多少分钟
     * @param Date1
     * @param Date2
     * @return long 返回值为：分钟数
     */  
    public static long getDiffTimeMinutes(Date one, Date two) {  
     
        long min = 0;  
//        long sec = 0;  
        try {  
            long time1 = one.getTime();  
            long time2 = two.getTime();  
            long diff ;  
            if(time1<time2) {  
                diff = time2 - time1;  
            } else {  
                diff = time1 - time2;  
            }  
            min = ((diff / (60 * 1000)));  
           // sec = (diff/1000-day*24*60*60-hour*60*60-min*60);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        //return day + "天" + hour + "小时" + min + "分" + sec + "秒";  
        return  min;
    }
    
    /** 
     * 两个时间相差距离多少毫秒数
     * @param Date1
     * @param Date2
     * @return String 返回值为：多少毫秒的字符串
     */  
    public static String getDistanceTime2(Date nowDate, Date endDate) { 
    	 
             long time1 = nowDate.getTime();  
             long time2 = endDate.getTime();  
             long diff ;  
             diff = time2 - time1;
            /* if(time1<time2) {  
                 diff = time2 - time1;  
             } else {  
                 diff = time1 - time2;  
             }  */
    	return String.valueOf(diff);
    }
    
    /** 
     * 两个时间相差距离多少毫秒数
     * @param Date1
     * @param Date2
     * @return String 返回值为：多少毫秒的字符串
     */  
    public static String getDistanceTimeMilli(Date nowDate, Date endDate) { 
    	 
             long time1 = nowDate.getTime();  
             long time2 = endDate.getTime();  
             long diff ;  
             diff = time2 - time1;
            /* if(time1<time2) {  
                 diff = time2 - time1;  
             } else {  
                 diff = time1 - time2;  
             }  */
    	return String.valueOf(diff);
    }
}
