package com.hc.core.utils.tool;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 获取当前的日期格式为:yyyy-MM-dd HH:mm:ss
 *
 * @author Arthur(user_006)
 * @version 1.0.0 2010/04/24 14:00(星期六)
 */
public class TimerUtil {

    /**
     * 获取当前的日期 , 默认格式
     *
     * @return 当前的日期
     */
    public synchronized static Date getCurrentCalendar() {

        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 获取当前日期 , 格式yyyy-MM-dd , 如: 2010-04-24
     *
     * @return
     */
    public synchronized static String getCurrentDate() {

        Calendar calendar = Calendar.getInstance();

        StringBuffer sb = new StringBuffer();

        sb.append(calendar.get(Calendar.YEAR)).append("-");
        sb.append(calendar.get(Calendar.MONTH) + 1).append("-");
        sb.append(calendar.get(Calendar.DAY_OF_MONTH));
        return sb.toString();
    }

    //获取前一天日期开始
    public synchronized static String getBeforeBeginDateTime() {
        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
        dBefore = calendar.getTime(); //得到前一天的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前一天
        defaultStartDate = defaultStartDate+" 00:00:00";
        return defaultStartDate;
    }
    //获取前一天日期结束
    public synchronized static String getBeforeEndDateTime() {

        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.DAY_OF_MONTH, -1); //设置为前一天
        dBefore = calendar.getTime(); //得到前一天的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前一天
        String defaultEndDate = defaultStartDate+" 23:59:59";
        return defaultEndDate;
    }

    public synchronized static String getCurrentBeginDateTime() {

        Calendar calendar = Calendar.getInstance();

        StringBuffer sb = new StringBuffer();

        sb.append(calendar.get(Calendar.YEAR)).append("-");
        sb.append(calendar.get(Calendar.MONTH) + 1).append("-");
        sb.append(calendar.get(Calendar.DAY_OF_MONTH));
        return sb.toString()+" 00:00:00";
    }

    public synchronized static String getCurrentEndDateTime() {

        Calendar calendar = Calendar.getInstance();

        StringBuffer sb = new StringBuffer();

        sb.append(calendar.get(Calendar.YEAR)).append("-");
        sb.append(calendar.get(Calendar.MONTH) + 1).append("-");
        sb.append(calendar.get(Calendar.DAY_OF_MONTH));
        return sb.toString()+" 23:59:59";
    }

    /**
     * 把字符形式的日期转换成Date类型 , 格式yyyy-MM-DD
     *
     * @param date
     *            日期的字符串形式
     *
     * @return Data类型的日期
     */
    public synchronized static Date convertToDate(String date) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-DD");

        try {

            return format.parse(date);

        } catch (ParseException e) {

            e.printStackTrace();
        }
        return null;
    }

    /**
     * 把字符形式的日期转换成Str , 格式yyyy-MM-DD
     *
     将2013-12-27 08:50:09
     *    解析为:yyyyMMdd
     *
     * @return 指定格式的字符串
     */
    public synchronized static String convertToDateStr(String dateStr, String pattern) {

        try {
            if (!dateStr.contains(" ")) {
                dateStr = String.format("%s 00:00:00", dateStr);
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // SimpleDateFormat的parse(String time)方法将String转换为Date
            Date date = simpleDateFormat.parse(dateStr);
            simpleDateFormat = new SimpleDateFormat(pattern);
            // SimpleDateFormat的format(Date date)方法将Date转换为String
            String formattedTime = simpleDateFormat.format(date);
            return formattedTime;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 把日期转换为指定的格式
     *
     * @param date
     *            要转换的日期
     * @param pattern
     *            转换的格式
     * @return
     */
    public synchronized static Date format(Date date, String pattern) {

        if (pattern != null && pattern.trim().length() > 0) {

            DateFormat format = new SimpleDateFormat(pattern);

            String stringDate = format.format(date);

            return TimerUtil.convertToDate(stringDate);
        }
        return null;
    }


    /**
     * 将日期转换为字符串 , 格式yyyy-MM-dd HH:mm:ss
     *
     * @param date
     *            要转换的日期
     * @return
     */
    public synchronized static String convertToStrShort(Date date) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 将日期转换为字符串 , 格式yyyyMMddHHmmss
     *
     * @param date
     *            要转换的日期
     * @return
     */
    public synchronized static String  convertToString2(Date date) {
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }


    /**
     * 将日期转换为指定格式的字符串
     *
     * @param date
     *            要转换的日期
     * @param formatText
     *            转换的格式
     * @return
     */
    public synchronized static String convertToString(Date date,
                                                      String formatText) {

        DateFormat format = new SimpleDateFormat(formatText);

        Calendar calendar = Calendar.getInstance();

        StringBuffer sb = new StringBuffer();

        sb.append(calendar.get(Calendar.YEAR)).append("-");
        sb.append(calendar.get(Calendar.MONTH) + 1).append("-");
        sb.append(calendar.get(Calendar.DAY_OF_MONTH));

        return format.format(date);
    }

    /**
     * 获取本周一的日期
     *
     * @return
     */
    public synchronized static Date getMondayOFWeek() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);// 这里设置从周一開始,若须要依据系统时区自己主动获取，则採用下边的方式

        return calendar.getTime();
    }

    /**
     * 获取本周日日期
     *
     * @return
     */
    public synchronized static Date getCurrentWeekday() {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, getCurrentPlus());// 把当前日期的DATE加上当前日期与本周日之间相差的天数
        return calendar.getTime();
    }

    /**
     * 获取上周日的日期
     *
     * @return
     */
    public synchronized static Date getPreviousWeekday() {

        Calendar calendar = Calendar.getInstance();
        // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7);
        calendar.add(Calendar.DATE, getCurrentPlus() - 7);// 把当前日期的DATE加上当前日期与本周日之间相差的天数
        return calendar.getTime();
    }

    /**
     * 获取上周日的日期
     *
     * @return
     */
    public synchronized static Date getPreviousMonday() {

        Calendar calendar = Calendar.getInstance();
        // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7);
        calendar.add(Calendar.DATE, getCurrentPlus() - 13);// 把当前日期的DATE加上当前日期与本周日之间相差的天数
        return calendar.getTime();
    }

    /**
     * 获取上周日的日期
     *
     * @return
     */
    public synchronized static Date getNextWeekday() {

        Calendar calendar = Calendar.getInstance();
        // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7);
        calendar.add(Calendar.DATE, getCurrentPlus() + 1 + 6);// 把当前日期的DATE加上当前日期与本周日之间相差的天数
        return calendar.getTime();
    }

    /**
     * 获取上周日的日期
     *
     * @return
     */
    public synchronized static Date getNextMonday() {

        Calendar calendar = Calendar.getInstance();
        // calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY+7);
        calendar.add(Calendar.DATE, getCurrentPlus() + 1);// 把当前日期的DATE加上当前日期与本周日之间相差的天数
        return calendar.getTime();
    }

    /**
     * 获取当前日期与本周周日之间相差的天数
     *
     * @return
     */
    public synchronized static int getCurrentPlus() {

        Calendar calendar = Calendar.getInstance();
        int days = calendar.get(Calendar.DAY_OF_WEEK) - 1;// 在中国是已星期一作为一周的第一天,所以这里减1
        return 7 - days;
    }

    public synchronized static int getCurrentYear() {

        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 返回指定日期的当月第一天
     *
     * @param date
     * @return
     */
    @SuppressWarnings("static-access")
    public synchronized static Date getFirstDayInMonth(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 返回指定日期的当月最后一天
     *
     * @param date
     * @return
     */
    @SuppressWarnings("static-access")
    public synchronized static Date getLastDayInMonth(Date date) {

        Calendar calendar = Calendar.getInstance();

        calendar.setTime(date);
        calendar.add(calendar.MONTH, 1);
        calendar.set(calendar.DATE, 1);
        calendar.add(calendar.DATE, -1);

        return calendar.getTime();
    }

    /*
     * 计算两个日期之间的天数
     */
    public static int getDaysBetween(Calendar d1, Calendar d2) {
        if (d1.after(d2)) {
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);// 得到当年的实际天数
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }
    /*
     * 计算两个日期之间的时间
     */
    public static String getGapTime(Date createTime) {
        // TODO Auto-generated method stub
        Calendar d2 = Calendar.getInstance();
        Calendar d1 = Calendar.getInstance();
        d1.setTime(createTime);
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        if (days < 1) {
            int hour = d2.get(Calendar.HOUR_OF_DAY)
                    - d1.get(Calendar.HOUR_OF_DAY);
            if (hour < 1) {
                int munites = d2.get(Calendar.MINUTE) - d1.get(Calendar.MINUTE);
                if (munites == 0) {
                    return "刚刚";
                } else {
                    return munites + "分钟前";
                }
            } else {
                return hour + "小时前";
            }

        } else {
            return days + "天前";
        }
    }

    //加天数
    public static String addSecond(int second){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        date.setTime(date.getTime() + (second*1000));
        return df.format(date);
    }

    //加天数
    public static String addDays(int day){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, day);
        StringBuffer sb = new StringBuffer();
        sb.append(calendar.get(Calendar.YEAR));
        if((calendar.get(Calendar.MONTH) + 1)<10)
        {
            sb.append("0").append(calendar.get(Calendar.MONTH) + 1);
        }
        else {
            sb.append(calendar.get(Calendar.MONTH) + 1);
        }
        if((calendar.get(Calendar.DAY_OF_MONTH))<10) {
            sb.append("0").append(calendar.get(Calendar.DAY_OF_MONTH));
        }else {
            sb.append(calendar.get(Calendar.DAY_OF_MONTH));
        }
        return sb.toString();
    }

    //加年份
    public static String addYears(String now, int year) throws ParseException{
        Calendar fromCal=Calendar.getInstance();

        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=dateFormat.parse(now);
        fromCal.setTime(date);
        fromCal.add(Calendar.YEAR, year);

        return dateFormat.format(fromCal.getTime());
    }

    //加天数(特定时间)
    public static String addDate(String now, int day) throws ParseException{
        Calendar fromCal=Calendar.getInstance();

        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=dateFormat.parse(now);
        fromCal.setTime(date);
        fromCal.add(Calendar.DATE, day);

        return dateFormat.format(fromCal.getTime());
    }

    /**
     * 获取毫秒时间戳
     *
     * @return 毫秒时间戳
     */
    public static Long getMillisecondTimestamp() {
        return System.currentTimeMillis();
    }

    /**
     * 获取毫秒时间戳
     *
     * @return 毫秒时间戳
     */
    public static Long getMillisecondTimestampAS(Long millisecond) {
        Long time = System.currentTimeMillis();
        time = time +millisecond;
        return time;
    }

    //时间戳转日期格式
    public static String timeStamp2Date(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    //时间戳转日期格式
    public static String timeStamp2Date(Long time) {
        if(time==null)
        {
            return "";
        }
        Long timeLong = Long.parseLong(time.toString());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将时间戳转换为时间,参数和返回值都是字符串
     * @param time
     * @return res
     */
    public static Long dateStrToStamp(String time) throws ParseException {
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
        Long time1 = parse.getTime();
        return time1;
    }


    /**
     * 格多少时间
     * @throws ParseException
     */

//    @SuppressWarnings("deprecation")
//    public static void main(String[] args) throws ParseException {
//        System.out.println(TimerUtil.addYears("2008-12-01",7));
//    }
		/*public static void main(String[] args) {
			   String createDate = "2008-12-01";
			   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
			   try {
			    Date date = sdf.parse(createDate);
			    Calendar cl = Calendar.getInstance();
			    cl.setTime(date);
			    cl.add(Calendar.DATE,31);
			    String temp = "";
			    temp = sdf.format(cl.getTime());
			    System.out.println(temp);
			   } catch (ParseException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			   }
			}*/
}