package com.xt.hb.data.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @version 0.1
 * @author myhnuhai
 * 
 */
public class DateUtil {

    private final static Locale locale = Locale.CHINA;
    public final static  int DAY_MILLISECOND = 86400000;
    private final static SimpleDateFormat sdf = new SimpleDateFormat();// 定义格式，不显示毫秒

    public final static String DATE_DAY_FMT = "yyyy-MM-dd";
    public final static String DATE_DAY_FMT_CN = "yyyy年MM月dd日";
    public final static String DATE_HOUR_FMT = "yyyy-MM-dd HH:00:00";
    public final static String DATE_MIN_FMT = "yyyy-MM-dd HH:mm:00";
    public final static String DATE_SECOND_FMT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_TIME = "yyyyMMddHHmmss";
    public final static String WEEK = "EEEE";

    /**
     * 将Date类型转换为字符串
     *
     * @param date
     *            日期类型
     * @return 日期字符串
     */
    public static String format(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将Date类型转换为字符串
     *
     * @param date
     *            日期类型
     * @param pattern
     *            字符串格式
     * @return 日期字符串
     */
    public static String format(Date date, String pattern) {
        if (date == null) {
            return "null";
        }
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param date
     *            字符串类型
     * @return 日期类型
     */
    public static Date format(String date) {
        return format(date, null);
    }

    /**
     * 将字符串转换为Date类型
     *
     * @param date
     *            字符串类型
     * @param pattern
     *            格式
     * @return 日期类型
     */
    public static Date format(String date, String pattern) {
        if (pattern == null || pattern.equals("") || pattern.equals("null")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        if (date == null || date.equals("") || date.equals("null")) {
            return new Date();
        }
        Date d = null;
        try {
            d = new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException pe) {
        }
        return d;
    }



    /**
     * 传入的参数格式为： yyyy-MM-dd HH:mm:ss
     *
     * @category 字母 日期或时间元素 表示 示例 <br/>
     *           G Era 标志符 Text AD <br/>
     *           y 年 Year 1996; 96 <br/>
     *           M 年中的月份 Month July; Jul; 07 <br/>
     *           w 年中的周数 Number 27 <br/>
     *           W 月份中的周数 Number 2 <br/>
     *           D 年中的天数 Number 189 <br/>
     *           d 月份中的天数 Number 10 <br/>
     *           F 月份中的星期 Number 2 <br/>
     *           E 星期中的天数 Text Tuesday; Tue <br/>
     *           a Am/pm 标记 Text PM <br/>
     *           H 一天中的小时数（0-23） Number 0 <br/>
     *           k 一天中的小时数（1-24） Number 24 <br/>
     *           K am/pm 中的小时数（0-11） Number 0 <br/>
     *           h am/pm 中的小时数（1-12） Number 12 <br/>
     *           m 小时中的分钟数 Number 30 <br/>
     *           s 分钟中的秒数 Number 55 <br/>
     *           S 毫秒数 Number 978 <br/>
     *           z 时区 General time zone Pacific Standard Time; PST; GMT-08:00 <br/>
     *           Z 时区 RFC 822 time zone -0800 <br/>
     *
     * @param date
     * @return Timestamp
     */
    public static Timestamp str2Timestamp(String date) {
        Timestamp returnt = Timestamp.valueOf(date);
        return returnt;
    }

    /**
     * Timestamp转换为String形式输出
     *
     * @param timestamp
     * @param dfm
     * @return String
     * @category 转字符串的时候如果传入的参数dformat为null，则默认以yyyy-MM-dd
     *           HH:mm:ss格式转换。否则按传入格式转。
     *
     */
    public static String timestamp2Str(Timestamp timestamp, String dfm) {
        String returnstr = "";
        if (dfm == null) {
            sdf.applyPattern(DATE_SECOND_FMT);
        } else {
            sdf.applyPattern(dfm);
        }
        returnstr = sdf.format(timestamp);
        return returnstr;
    }

    /**
     * Date 转为 String形式输出
     *
     * @param date
     * @param dfm
     * @return String
     * @category 转字符串的时候如果传入的参数dformat为null，则默认以yyyy-MM-dd
     *           HH:mm:ss格式转换。否则按传入格式转。
     */
    public static String date2Str(Date date, String dfm) {
        if (dfm == null) {
            sdf.applyPattern(DATE_SECOND_FMT);
        } else {
            sdf.applyPattern(dfm);
        }
        String returnstr = sdf.format(date);
        return returnstr;
    }

    /**
     * 获取今天是周几，也就是一星期中的第几天。
     *
     * @return int
     */
    public static int getWeek() {

        Calendar calendar = Calendar.getInstance(locale);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);

        dayofweek = (dayofweek == 1) ? 7 : (dayofweek - 1);
        return dayofweek;
    }

    /**
     * 获取今天是一年中的第几周。从年初开始算。
     *
     * @return int
     */
    public static int getWeekofYear() {
        Calendar c = Calendar.getInstance(locale);
        c.setTime(new Date());

        int returnint = c.get(Calendar.WEEK_OF_YEAR);
        return returnint;
    }

    /**
     * 获取年份
     *
     * @return int
     */

    public static int getYear() {
        Calendar c = Calendar.getInstance(locale);
        c.setTime(new Date());
        int returnint = c.get(Calendar.YEAR);
        return returnint;
    }

    /**
     * 获取月份
     *
     * @return int
     */
    public static int getMonth() {
        Calendar c = Calendar.getInstance(locale);

        c.setTime(new Date());

        int returnint = c.get(Calendar.MONTH) + 1;
        return returnint;
    }

    /**
     * 字符串转Date
     * @param date
     * @param fmt
     * @return
     */
    public static Date str2Date(String date, String fmt) {

        if (fmt == null) {
            sdf.applyPattern(DATE_SECOND_FMT);
        } else {
            sdf.applyPattern(fmt);
        }
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前季度
     * @return
     */
    public static Integer getQuarter() {
        Calendar calendar = Calendar.getInstance(locale);
        int month = calendar.get(Calendar.MONDAY) + 1;

        if (month == 1 || month == 2 || month == 3) {
            return 1;
        }
        if (month == 4 || month == 5 || month == 6) {
            return 2;
        }
        if (month == 7 || month == 8 || month == 9) {
            return 3;
        }
        return 4;
    }

    public static Date getFirstDayOfMonth(){
        Calendar calendar = Calendar.getInstance(locale);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

        return calendar.getTime();
    }

    public static Date getFirstDay(int year,int month){
        Calendar calendar = Calendar.getInstance(locale);
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

        return calendar.getTime();
    }

    public static Date getLastDay(int year,int month){
        Calendar calendar = Calendar.getInstance(locale);
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 获取上周周一的日期
     * @return
     */
    public static Date getPreviouFirstWeek(){
        Calendar calendar = Calendar.getInstance(locale);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK,  Calendar.MONDAY);
        calendar.add(Calendar.DAY_OF_WEEK, -7);
        return calendar.getTime();
    }

    /**
     * 获取本周周一的日期
     * @return
     */
    public static Date getFirstOfWeek(){
        Calendar calendar = Calendar.getInstance(locale);
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return calendar.getTime();
    }

    /**
     * 获取今天的日期
     * @return
     */
    public static Date getToDay(){
        return Calendar.getInstance(locale).getTime();
    }


    /**
     * 当前季度的开始时间，即2012-01-1 00:00:00
     *
     * @return
     */
    public static   Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }


    public static Date getPreviouQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3){
                c.set(Calendar.MONTH, 9);
                c.add(Calendar.YEAR,-1);
            }else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 6);
            c.set(Calendar.DATE, 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return c.getTime();
    }

}
