package org.smartframework.common.utils;


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

public class ExDateUtils {
    private static String dateFormat = "yyyy-MM-dd";
    private static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
    private static String timeFormat = "HH:mm:ss";
//    private static DateFormat shortDateTimeFormat = new SimpleDateFormat("yyyy-M-d HH:mm");
//    private static DateFormat shortDayTimeFormat = new SimpleDateFormat("M-d HH:mm");
//    private static DateFormat shortTimeFormat = new SimpleDateFormat("HH:mm");
//    private static DateFormat shortDateFormat = new SimpleDateFormat("MM-dd");
//    private static DateFormat yearMonthDateFormat = new SimpleDateFormat("yyyy-MM");
//    private static DateFormat periodDateFormat = new SimpleDateFormat("yyyyMM");

    public ExDateUtils() {
    }

    public static String getToday() {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        Date currentDate = new Date();
        return formatter.format(currentDate);
    }

    public static String getLastMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, -1);
        return formatter.format(calendar.getTime());
    }

//    public static String formatYearMonth(Date date) {
//        return date != null && yearMonthDateFormat != null?yearMonthDateFormat.format(date):null;
//    }
//
//    public static String formatPeriod(Date date) {
//        return date != null && periodDateFormat != null?periodDateFormat.format(date):null;
//    }
//
//    public static String formatShortDate(Date date) {
//        return date != null && shortDateFormat != null?shortDateFormat.format(date):null;
//    }

    public static String formatDate(Date date) {
        if(date == null){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date);
    }

    public static String formatDateTime(Date date) {
        if(date == null){
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(dateTimeFormat);
        return formatter.format(date);
    }

//    public static String formatShortDateTime(Date date) {
//        return date != null && shortDateTimeFormat != null?shortDateTimeFormat.format(date):null;
//    }
//
//    public static String formatShortDayTime(Date date) {
//        return date != null && shortDayTimeFormat != null?shortDayTimeFormat.format(date):null;
//    }

    public static String formatTime(Date date) {
        if(date == null){
            return null;
        }
        DateFormat tmpDateFormat = new SimpleDateFormat(timeFormat);
        return tmpDateFormat.format(timeFormat);
    }

//    public static String formatShortTime(Date date) {
//        return date != null && shortTimeFormat != null?shortTimeFormat.format(date):null;
//    }

//    public static String formatDateOrTime(Date date) {
//        if(date != null && timeFormat != null) {
//            Date today = new Date();
//            return getMonth(date) == getMonth(today) && getMonthDay(date) == getMonthDay(today)?shortTimeFormat.format(date):shortDateFormat.format(date);
//        } else {
//            return null;
//        }
//    }

    public static String format(Date date, String pattern) {
        if(date != null && pattern != null) {
            DateFormat tmpDateFormat = new SimpleDateFormat(pattern);
            return tmpDateFormat.format(date);
        } else {
            return null;
        }
    }

    public static Date getTodayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getSysDateTime() {
        return new Date();
    }

    public static Date getBeginningOfWeek(Date beginDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(beginDate);

        int dayOfWeek;
        for(dayOfWeek = calendar.get(7); dayOfWeek > calendar.getFirstDayOfWeek(); dayOfWeek = calendar.get(7)) {
            calendar.add(5, -1);
        }

        if(dayOfWeek < calendar.getFirstDayOfWeek()) {
            calendar.add(5, 1);
        }

        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getBeginningOfWeek() {
        return getBeginningOfWeek(new Date());
    }

    public static Date getEndOfWeek(Date aDate) {
        Date tmpDate = getBeginningOfWeek(aDate);
        tmpDate = addDay(tmpDate, 6);
        return tmpDate;
    }

    public static Date getEndOfWeek() {
        Date tmpDate = getBeginningOfWeek();
        tmpDate = addDay(tmpDate, 6);
        return tmpDate;
    }

    public static Date getBeginningOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2, 0);
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getBeginningOfYear(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(1, year);
        calendar.set(2, 0);
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getBeginningOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getEndOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, 1);
        calendar.set(5, 1);
        calendar.add(5, -1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static Date getEndOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, month);
        calendar.set(5, 1);
        calendar.add(5, -1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 0);
        return calendar.getTime();
    }

    public static String getEndOfMonth(String startDate) {
        Date sDate = parseDate(startDate);
        if(sDate == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(sDate);
            calendar.add(2, 1);
            calendar.set(5, 1);
            calendar.add(5, -1);
            return formatDate(calendar.getTime());
        }
    }

    public static Date parseDate(String dateStr, String datePattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);

        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException var4) {
            System.out.println(var4.getMessage());
            return null;
        }
    }

    public static Date parseDate(String dateStr) {
        DateFormat dateFormat = null;
        if(dateStr.indexOf("CST") >= 0) {
            dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        } else if(dateStr.indexOf("CDT") >= 0) {
            dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        } else if(dateStr.indexOf("UTC") >= 0) {
            dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC'Z yyyy", Locale.US);
        } else if(dateStr.indexOf("GMT") >= 0) {
            dateStr = dateStr.replace("GMT+0800", "GMT+08:00");
            dateFormat = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.US);
        } else if(dateStr.indexOf("-") < 0 && dateStr.length() == 8) {
            dateFormat = new SimpleDateFormat("yyyyMMdd");
        } else if(dateStr.indexOf(":") > 0 && dateStr.indexOf("-") > 0 && dateStr.length() == 19) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else if(dateStr.indexOf(":") > 0 && dateStr.indexOf("-") > 0 && dateStr.length() == 16) {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        } else if(dateStr.indexOf(":") > 0 && dateStr.indexOf("/") > 0 && dateStr.length() == 19) {
            dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        } else if(dateStr.indexOf(":") > 0 && dateStr.indexOf("/") > 0 && dateStr.length() == 16) {
            dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        } else if(dateStr.indexOf("/") > 0 && dateStr.length() >= 8 && dateStr.length() <= 10) {
            dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        } else {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        }

        try {
            return dateFormat.parse(dateStr);
        } catch (ParseException var3) {
            System.out.println(var3.getMessage());
            return null;
        }
    }

    public static Date parseDate(long time) {
        if(time > 0L) {
            Date tmpDate = new Date();
            tmpDate.setTime(time);
            return tmpDate;
        } else {
            return null;
        }
    }

    public static int getYear(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        return calendar.get(1);
    }

    public static int getMonth(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        return calendar.get(2) + 1;
    }

    public static int getMonthDay(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        return calendar.get(5);
    }

    public static int getWeekDay(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        int weekDay = calendar.get(7);
        --weekDay;
        if(weekDay == 0) {
            weekDay = 7;
        }

        return weekDay;
    }

    public static int getHourOfDay(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        return calendar.get(11);
    }

    public static int getMinute(Date sDate) {
        if(sDate == null) {
            sDate = new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sDate);
        return calendar.get(12);
    }

    public static String getZhWeekDay(Date sDate) {
        int weekDay = getWeekDay(sDate);
        return weekDay == 1?"一":(weekDay == 2?"二":(weekDay == 3?"三":(weekDay == 4?"四":(weekDay == 5?"五":(weekDay == 6?"六":(weekDay == 7?"日":null))))));
    }

    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, month);
        return calendar.getTime();
    }

    public static Date addDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, days);
        return calendar.getTime();
    }

    public static Date addMinute(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(12, minutes);
        return calendar.getTime();
    }

    public static boolean isBefore(Date leftDate, Date rightDate) {
        return leftDate.before(rightDate);
    }

    public static boolean isAfter(Date leftDate, Date rightDate) {
        return leftDate.after(rightDate);
    }

    public static void main(String[] args) {
        Date today = new Date();
        System.out.println(today);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(dateFormat.format(today));
        dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        System.out.println(dateFormat.format(today));
        dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'UTC'Z yyyy", Locale.US);
        System.out.println(dateFormat.format(today));
        Date g = getBeginningOfWeek(parseDate("2011-5-7"));
        System.out.println("ccc:" + formatDate(g));
        g = getBeginningOfWeek(parseDate("2011-5-8"));
        System.out.println("ccc:" + formatDate(g));
        g = getBeginningOfWeek(parseDate("2011-5-9"));
        System.out.println("ccc:" + formatDate(g));
    }

    public static Date setTime(Date date, String time) {
        Date tmpDate = date;
        if(date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            String[] strArr = ExStringUtils.split(time, ":");
            int hour = 0;
            int minute = 0;
            int second = 0;
            if(strArr != null && strArr.length == 2) {
                hour = Integer.parseInt(strArr[0]);
                minute = Integer.parseInt(strArr[1]);
            } else if(strArr != null && strArr.length == 3) {
                hour = Integer.parseInt(strArr[0]);
                minute = Integer.parseInt(strArr[1]);
                second = Integer.parseInt(strArr[2]);
            }

            calendar.set(10, hour);
            calendar.set(12, minute);
            calendar.set(13, second);
            calendar.set(14, 0);
            tmpDate = calendar.getTime();
            date = calendar.getTime();
        }

        return tmpDate;
    }

    public static int getWeekNum(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(3);
    }

    public static int getDiffDay(Date startTime, Date endTime) {
        return (int)dateDiff(startTime, endTime)[0];
    }

    public static long[] dateDiff(Date startTime, Date endTime) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long ns = 1000L;
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        long sec = diff % nd % nh % nm / ns;
        long[] times = new long[]{day, hour, min, sec};
        return times;
    }
}
