package cn.com.liandisys.ycjt.ggcx.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.math.RandomUtils;

/**
 * 日期处理辅助类
 * @author wangqx
 *
 */
public class DateUtil {

    /** yyyy-MM-dd */
    public static final String YEAR_TO_DAY = "yyyy-MM-dd";

    /** yyyy-MM-dd HH:mm:ss */
    public static final String YEAR_TO_SECOND = "yyyy-MM-dd HH:mm:ss";

    public static String formatDate(String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    public static String formatDate(Date date, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String formatTomorrowDate(String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return sdf.format(calendar.getTime());
    }

    public static int compare(Date date1, Date date2) {

        return date1.compareTo(date2);
    }

    public static int compare(String date1, String format, Date date2) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = sdf.parse(date1);
        return date.compareTo(date2);
    }

    public static Date parse(String date, String format) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    public static Date parse(String date, String format, Locale locale, TimeZone timeZone) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
        sdf.setTimeZone(timeZone);
        return sdf.parse(date);
    }

    public static Date getNextDate(String hourToSecond) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_DAY);
        Date date = sdf.parse(DateUtil.formatDate(YEAR_TO_DAY) + " " + hourToSecond);
        return date;
    }

    public static Date getCurrentDateRandomSecond(String hourToSecond) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);
        String[] fields = hourToSecond.split("\\:");
        Integer randomSecond = RandomUtils.nextInt(59);
        String second = (randomSecond.toString().length() == 1 ? "0" + randomSecond : randomSecond + "");
        String date = DateUtil.formatDate(YEAR_TO_DAY) + " " + fields[0] + ":" + fields[1] + ":" + second;
        return sdf.parse(date);
    }

    public static Date getNextDateRandomSecond(String hourToSecond) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat(YEAR_TO_SECOND);
        String[] fields = hourToSecond.split("\\:");
        Integer randomSecond = RandomUtils.nextInt(59);
        String second = (randomSecond.toString().length() == 1 ? "0" + randomSecond : randomSecond + "");
        String date = DateUtil.formatTomorrowDate(YEAR_TO_DAY) + " " + fields[0] + ":" + fields[1] + ":" + second;
        return sdf.parse(date);
    }

    public static String formatDateCHS(Date date) {

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        StringBuilder str = new StringBuilder();
        str.append(calendar.get(Calendar.YEAR));
        str.append("年");
        str.append(calendar.get(Calendar.MONTH) + 1);
        str.append("月");
        str.append(calendar.get(Calendar.DAY_OF_MONTH));
        str.append("日");
        return str.toString();
    }

    /**
     * 获取传入参数之后的？天
     * 
     * @param today
     *            传入的当前天
     * @param i
     *            需要相加的天数
     * @return
     */
    @SuppressWarnings("static-access")
    public static String getNextDay(String today, int i) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dd = new Date();
        try {
            dd = formatter.parse(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dd);
        calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        String dateString = formatter.format(dd);
        return dateString;
    }

    /**
     * 获取传入参数之后的？天
     * 
     * @param today
     *            传入的当前天
     * @param i
     *            需要相加的天数
     * @return
     */
    @SuppressWarnings("static-access")
    public static Date getNextDayonDate(String today, int i) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dd = new Date();
        try {
            dd = formatter.parse(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dd);
        calendar.add(calendar.DATE, i);// 把日期往后增加一天.整数往后推,负数往前移动
        dd = calendar.getTime(); // 这个时间就是日期往后推一天的结果
        return dd;
    }

    public static Date getNextDay(Date date, int i) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, i);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取指定周期之前的时刻
     * @param endTime
     * @param period
     * @return
     */
    public static long getBeforPeriodTimestamp(long endTime, DateUtil.Period period) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(endTime);

        calendar.add(period.getCalendarField(), 0 - period.getAmount());
        return calendar.getTimeInMillis();
    };

    /**
     * 获取指定周期之前的时长
     * @param endTime
     * @param period
     * @return
     */
    public static long getBeforPeriodDuration(long endTime, DateUtil.Period period) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(endTime);

        calendar.add(period.getCalendarField(), 0 - period.getAmount());
        return endTime - calendar.getTimeInMillis();
    };

    public enum Period {
        /** 一天 */
        ONE_DAY("一天", Calendar.DAY_OF_MONTH, 1),
        /** 一天 */
        TWO_DAY("两天", Calendar.DAY_OF_MONTH, 2),
        /** 一天 */
        THREE_DAY("三天", Calendar.DAY_OF_MONTH, 3),
        /** 一天 */
        ONE_WEEK("一星期", Calendar.WEEK_OF_MONTH, 1),
        /** 一天 */
        TWO_WEEK("两星期", Calendar.WEEK_OF_MONTH, 2),
        /** 一天 */
        THREE_WEEK("三星期", Calendar.WEEK_OF_MONTH, 3),
        /** 一天 */
        ONE_MONTH("一月", Calendar.MONTH, 1);

        private Period(String name, int calendarField, int amount) {
            this.name = name;
            this.calendarField = calendarField;
            this.amount = amount;
        }

        private String name;

        private int calendarField;

        private int amount;

        public String getName() {

            return this.name;
        }

        public int getCalendarField() {

            return calendarField;
        }

        public int getAmount() {

            return amount;
        }
    }

    /**
     * @param time
     * @param format
     * @return
     */
    public static String format(Long time, String format) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return DateUtil.formatDate(calendar.getTime(), format);
    }

    public static boolean isWorkDay(Calendar calendar) {

        return !(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
    }

    /**
     * @param date
     * @return
     */
    public static boolean isWorkDay(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return isWorkDay(c);
    }
    
    /**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000);
	}
}