/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang3.StringUtils
 */
package com.hst.framework.util;

import com.hst.framework.constants.WebConstants;
import java.sql.Timestamp;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.commons.lang3.StringUtils;

public final class HstDateUtil {
    public static final String DATEFORMAT = "yyyy-MM-dd";
    public static final String TIME_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static long intervalSecs;
    private static final long milisPerHour = 3600000L;

    public static java.sql.Date getSqlDate() throws Exception {
        Date currentDate = new Date();
        return java.sql.Date.valueOf(HstDateUtil.format(currentDate, DATEFORMAT));
    }

    public static java.sql.Date getSqlDate(Date date) throws Exception {
        if (date == null) {
            return null;
        }
        return java.sql.Date.valueOf(HstDateUtil.format(date, DATEFORMAT));
    }

    public static String format(Date date, String format) throws Exception {
        if (date == null) {
            return "";
        }
        if (format == null) {
            format = DATEFORMAT;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static String format(Date date) throws Exception {
        return HstDateUtil.format(date, null);
    }

    public static String getIntervalDate(Date date, int field, int amount) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(DATEFORMAT);
            gc.setTime(date);
            gc.add(field, amount);
            return df.format(gc.getTime());
        }
        catch (Exception ex) {
            throw new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
        }
    }

    public static Date parse(String dateString, String format) throws Exception {
        try {
            if (format == null) {
                format = DATEFORMAT;
            }
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateString);
        }
        catch (Exception ex) {
            Exception ue = new Exception("\u683c\u5f0f\u5316\u65e5\u671f\u4ea7\u751f\u5f02\u5e38", ex);
            throw ue;
        }
        catch (Throwable t) {
            throw new Exception("\u683c\u5f0f\u5316\u65e5\u671f\u4ea7\u751f\u5f02\u5e38", t);
        }
    }

    public static Date parse(String dateString) throws Exception {
        return HstDateUtil.parse(dateString, null);
    }

    public static String getIntervalDate(String dateStr, int field, int amount) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(DATEFORMAT);
            Date date = df.parse(dateStr, new ParsePosition(0));
            gc.setTime(date);
            gc.add(field, amount);
            return df.format(gc.getTime());
        }
        catch (Exception ex) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
            throw ue;
        }
        catch (Throwable t) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", t);
            throw ue;
        }
    }

    public static String getIntervalDate(String dateStr, String format, int field, int amount) throws Exception {
        try {
            if (format == null) {
                format = DATEFORMAT;
            }
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(format);
            Date date = df.parse(dateStr, new ParsePosition(0));
            gc.setTime(date);
            gc.add(field, amount);
            return df.format(gc.getTime());
        }
        catch (Exception ex) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
            throw ue;
        }
        catch (Throwable t) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", t);
            throw ue;
        }
    }

    public static String getFirstDayMonth() throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(DATEFORMAT);
            gc.setTime(new Date());
            return df.format(gc.getTime()).substring(0, 8).concat("01");
        }
        catch (Exception ex) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
        }
        catch (Throwable t) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", t);
        }
    }

    public static String getFirstDayMonth(String date) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(DATEFORMAT);
            gc.setTime(HstDateUtil.parse(date));
            return df.format(gc.getTime()).substring(0, 8).concat("01");
        }
        catch (Exception ex) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
        }
        catch (Throwable t) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", t);
        }
    }

    public static String getFirstDayNextMonth(String date) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(DATEFORMAT);
            String nextDate = HstDateUtil.getIntervalDate(date, 2, 1);
            gc.setTime(HstDateUtil.parse(nextDate));
            return df.format(gc.getTime()).substring(0, 8).concat("01");
        }
        catch (Exception ex) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u4e0b\u4e2a\u6708\u4efd\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
        }
        catch (Throwable t) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u4e0b\u4e2a\u6708\u4efd\u7684\u7b2c\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", t);
        }
    }

    public static String getLastDayOfMonth(String date) throws Exception {
        try {
            String nextDayOfMonth = HstDateUtil.getFirstDayNextMonth(date);
            return HstDateUtil.getIntervalDate(nextDayOfMonth, 5, -1);
        }
        catch (Exception ex) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u6700\u540e\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
        }
        catch (Throwable t) {
            throw new Exception("\u83b7\u53d6\u5f53\u6708\u7684\u6700\u540e\u4e00\u5929\u65f6\u4ea7\u751f\u5f02\u5e38", t);
        }
    }

    public static String getYyyyMmdd(String rq) throws Exception {
        if (rq == null || "".equals(rq)) {
            return "";
        }
        return HstDateUtil.toDateString(HstDateUtil.parse(rq));
    }

    public static String getYyyyMmddHhMmSs(String rq) throws Exception {
        if (rq == null || "".equals(rq)) {
            return "";
        }
        return HstDateUtil.toDateString(HstDateUtil.parse(rq, TIME_DATETIME_FORMAT), TIME_DATETIME_FORMAT);
    }

    public static String toDateString(Date date) throws Exception {
        if (date == null) {
            return null;
        }
        try {
            return HstDateUtil.toDateString(date, DATEFORMAT);
        }
        catch (Exception e) {
            throw new IllegalArgumentException("\u65e5\u671f\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u5931\u8d25:" + e);
        }
    }

    public static String toDateString(Date date, String toFormat) throws Exception {
        GregorianCalendar gc = new GregorianCalendar();
        SimpleDateFormat df = new SimpleDateFormat(toFormat);
        df.setTimeZone(gc.getTimeZone());
        return df.format(date);
    }

    public static int getCurYear() {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(new Date());
        return gc.get(1);
    }

    public static int getCurMonth() {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(new Date());
        return gc.get(2);
    }

    public static int getCurDay() {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(new Date());
        return gc.get(5);
    }

    public static boolean dateCompare(Date baseDate, Date reqDate) throws Exception {
        boolean theFlag = false;
        if (baseDate == null) {
            baseDate = new Date();
        }
        if (reqDate.getTime() > baseDate.getTime()) {
            theFlag = true;
        }
        return theFlag;
    }

    public static boolean dateCompareMethod(Date baseDate, Date reqDate) throws Exception {
        boolean theFlag = false;
        if (baseDate == null) {
            baseDate = new Date();
        }
        if (reqDate.getTime() >= baseDate.getTime()) {
            theFlag = true;
        }
        return theFlag;
    }

    public static boolean dateCompare(String baseDate, String reqDate) throws Exception {
        Date bDate = null;
        bDate = baseDate == null || baseDate.trim().equals("") ? new Date() : HstDateUtil.parse(baseDate);
        Date rDate = HstDateUtil.parse(reqDate);
        return HstDateUtil.dateCompare(bDate, rDate);
    }

    public static String getCurrentDate() throws Exception {
        Date currentDay = new Date();
        return HstDateUtil.toDateString(currentDay, DATEFORMAT);
    }

    public static Timestamp getCurrentTime() throws Exception {
        return new Timestamp(System.currentTimeMillis());
    }

    public static Timestamp getTimestampTime(String dateStr) throws Exception {
        if (StringUtils.isNotBlank((CharSequence)dateStr) && dateStr.length() == 10) {
            dateStr = String.valueOf(dateStr) + " 00:00:00";
        }
        return new Timestamp(HstDateUtil.parse(dateStr, TIME_DATETIME_FORMAT).getTime());
    }

    public static String getFirstOfQuarter(String dateStr) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(HstDateUtil.parse(dateStr));
            int i = gc.get(2);
            switch (i) {
                case 0: 
                case 1: 
                case 2: {
                    i = 0;
                    break;
                }
                case 3: 
                case 4: 
                case 5: {
                    i = 3;
                    break;
                }
                case 6: 
                case 7: 
                case 8: {
                    i = 6;
                    break;
                }
                case 9: 
                case 10: 
                case 11: {
                    i = 9;
                }
            }
            gc.set(gc.get(1), i, 1);
            return HstDateUtil.toDateString(gc.getTime(), DATEFORMAT);
        }
        catch (Exception e) {
            throw new Exception("\u83b7\u53d6First Day Of Quarter\u51fa\u73b0\u5f02\u5e38", e);
        }
    }

    public static String getLastOfQuarter(String dateStr) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(HstDateUtil.parse(dateStr));
            int i = gc.get(2);
            switch (i) {
                case 0: 
                case 1: 
                case 2: {
                    i = 3;
                    break;
                }
                case 3: 
                case 4: 
                case 5: {
                    i = 6;
                    break;
                }
                case 6: 
                case 7: 
                case 8: {
                    i = 9;
                    break;
                }
                case 9: 
                case 10: 
                case 11: {
                    i = 0;
                }
            }
            if (i == 0) {
                gc.set(gc.get(1) + 1, i, 1);
            } else {
                gc.set(gc.get(1), i, 1);
            }
            String firstDayOfNextQuarter = HstDateUtil.toDateString(gc.getTime(), DATEFORMAT);
            return HstDateUtil.getIntervalDate(firstDayOfNextQuarter, 5, -1);
        }
        catch (Exception e) {
            throw new Exception("\u83b7\u53d6First Day Of Quarter\u51fa\u73b0\u5f02\u5e38", e);
        }
    }

    public static String getFirstOfQuarter(Date date) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            int i = gc.get(2);
            switch (i) {
                case 0: 
                case 1: 
                case 2: {
                    i = 0;
                    break;
                }
                case 3: 
                case 4: 
                case 5: {
                    i = 3;
                    break;
                }
                case 6: 
                case 7: 
                case 8: {
                    i = 6;
                    break;
                }
                case 9: 
                case 10: 
                case 11: {
                    i = 9;
                }
            }
            gc.set(gc.get(1), i, 1);
            return HstDateUtil.toDateString(gc.getTime(), DATEFORMAT);
        }
        catch (Exception e) {
            throw new Exception("\u83b7\u53d6First Day Of Quarter\u51fa\u73b0\u5f02\u5e38", e);
        }
    }

    public static String getLastOfQuarter(Date date) throws Exception {
        String first = HstDateUtil.getFirstOfQuarter(date);
        String last = HstDateUtil.getIntervalDate(HstDateUtil.getIntervalDate(first, 2, 3), 5, -1);
        return last;
    }

    public static String getFirstOfHalfYear(Date date) throws Exception {
        String result = HstDateUtil.format(date, null);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        int month = gc.get(2);
        String temp = "-01-01";
        temp = month < 6 ? "-01-01" : "-07-01";
        return result.substring(0, 4).concat(temp);
    }

    public static String getLastOfHalfYear(Date date) throws Exception {
        String result = HstDateUtil.format(date, null);
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        int month = gc.get(2);
        String temp = "-06-30";
        temp = month < 6 ? "-06-30" : "-12-31";
        return result.substring(0, 4).concat(temp);
    }

    public static String getFirstOfYear(Date date) throws Exception {
        String result = HstDateUtil.format(date, null);
        return result.substring(0, 4).concat("-01-01");
    }

    public static String getLastOfYear(Date date) throws Exception {
        String result = HstDateUtil.format(date, null);
        return result.substring(0, 4).concat("-12-31");
    }

    public static String[] getYearList() {
        return HstDateUtil.getYearList(HstDateUtil.getCurYear());
    }

    public static String[] getYearList(int year) {
        return HstDateUtil.getYearList(year, 5);
    }

    private static String[] getYearList(int year, int i) {
        String[] years = new String[2 * i];
        int mun = year + i;
        int j = 0;
        while (j < 2 * i) {
            years[j] = String.valueOf(mun - j);
            ++j;
        }
        return years;
    }

    public static int dateDiff(String dateStart, String dateEnd) throws Exception {
        int days;
        try {
            Date dStart = HstDateUtil.parse(dateStart);
            Date dEnd = HstDateUtil.parse(dateEnd);
            long diff = dEnd.getTime() - dStart.getTime();
            days = (int)(diff / 86400000L);
        }
        catch (Exception e) {
            throw new Exception("\u83b7\u53d6\u65e5\u671f\u4e4b\u95f4\u7684\u5929\u6570", e);
        }
        return days;
    }

    public static String getWorkingDay(String dateStr, int days) throws Exception {
        if (dateStr == null) {
            return null;
        }
        Date dateZ = HstDateUtil.parse(dateStr, DATEFORMAT);
        Calendar calendarZ = Calendar.getInstance();
        calendarZ.setTime(dateZ);
        int i = 1;
        while (i <= days) {
            String time;
            calendarZ.set(5, calendarZ.get(5) + 1);
            if (calendarZ.get(7) == 7 && (!WebConstants.GZR_BG_MAP.containsKey(time = HstDateUtil.format(calendarZ.getTime(), null)) || WebConstants.GZR_BG_MAP.containsKey(time) && WebConstants.GZR_BG_MAP.get(time).equals("N"))) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            if (calendarZ.get(7) == 1 && (!WebConstants.GZR_BG_MAP.containsKey(time = HstDateUtil.format(calendarZ.getTime(), null)) || WebConstants.GZR_BG_MAP.containsKey(time) && WebConstants.GZR_BG_MAP.get(time).equals("N"))) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            while (WebConstants.GZR_BG_MAP.containsKey(HstDateUtil.format(calendarZ.getTime(), null)) && WebConstants.GZR_BG_MAP.get(HstDateUtil.format(calendarZ.getTime(), null)).equals("N")) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            if (calendarZ.get(7) == 7 && (!WebConstants.GZR_BG_MAP.containsKey(time = HstDateUtil.format(calendarZ.getTime(), null)) || WebConstants.GZR_BG_MAP.containsKey(time) && WebConstants.GZR_BG_MAP.get(time).equals("N"))) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            if (calendarZ.get(7) == 1 && (!WebConstants.GZR_BG_MAP.containsKey(time = HstDateUtil.format(calendarZ.getTime(), null)) || WebConstants.GZR_BG_MAP.containsKey(time) && WebConstants.GZR_BG_MAP.get(time).equals("N"))) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            while (WebConstants.GZR_BG_MAP.containsKey(HstDateUtil.format(calendarZ.getTime(), null)) && WebConstants.GZR_BG_MAP.get(HstDateUtil.format(calendarZ.getTime(), null)).equals("N")) {
                calendarZ.set(5, calendarZ.get(5) + 1);
            }
            ++i;
        }
        String sMonth = calendarZ.get(2) + 1 < 10 ? "0" + (calendarZ.get(2) + 1) : String.valueOf(calendarZ.get(2) + 1);
        String sDay = calendarZ.get(5) < 10 ? "0" + calendarZ.get(5) : String.valueOf(calendarZ.get(5));
        return new StringBuffer().append(calendarZ.get(1)).append("-").append(sMonth).append("-").append(sDay).toString();
    }

    public static String getWorkingDayHours(String dateStr, int hours) throws Exception {
        if (dateStr == null) {
            return null;
        }
        if (intervalSecs == 0L) {
            intervalSecs = HstDateUtil.getWorkingSecs();
        }
        long totalSecs = HstDateUtil.getWorkingTotalSecs(dateStr, hours);
        long days = totalSecs / intervalSecs;
        long hour = totalSecs % intervalSecs / 3600L;
        Date dateZ = HstDateUtil.parse(dateStr, TIME_DATETIME_FORMAT);
        Calendar calendarZ = Calendar.getInstance();
        calendarZ.setTime(dateZ);
        if (days > 0L) {
            int i = 0;
            while ((long)i < days) {
                calendarZ.set(5, calendarZ.get(5) + 1);
                if (calendarZ.get(7) == 7) {
                    calendarZ.set(5, calendarZ.get(5) + 1);
                }
                if (calendarZ.get(7) == 1) {
                    calendarZ.set(5, calendarZ.get(5) + 1);
                }
                while (WebConstants.GZR_BG_MAP.containsKey(HstDateUtil.format(calendarZ.getTime()))) {
                    calendarZ.set(5, calendarZ.get(5) + 1);
                }
                ++i;
            }
            int sHour = Integer.parseInt(WebConstants.WORKING_TIME_Q.split(":")[0]);
            calendarZ.set(11, sHour + (int)hour + 1);
        } else {
            calendarZ.set(11, calendarZ.get(11) + hours);
        }
        String sMonth = calendarZ.get(2) + 1 < 10 ? "0" + (calendarZ.get(2) + 1) : String.valueOf(calendarZ.get(2) + 1);
        String sDay = calendarZ.get(5) < 10 ? "0" + calendarZ.get(5) : String.valueOf(calendarZ.get(5));
        String strHour = calendarZ.get(11) < 10 ? "0" + calendarZ.get(11) : String.valueOf(calendarZ.get(11));
        String strMinute = calendarZ.get(12) < 10 ? "0" + calendarZ.get(12) : String.valueOf(calendarZ.get(12));
        String strSecond = calendarZ.get(13) < 10 ? "0" + calendarZ.get(13) : String.valueOf(calendarZ.get(13));
        return new StringBuffer().append(calendarZ.get(1)).append("-").append(sMonth).append("-").append(sDay).append(" ").append(strHour).append(":").append(strMinute).append(":").append(strSecond).toString();
    }

    private static long getWorkingSecs() throws Exception {
        Date dateQ = HstDateUtil.parse(String.valueOf(HstDateUtil.getCurYear()) + "-" + HstDateUtil.getCurMonth() + "-" + HstDateUtil.getCurDay() + " " + WebConstants.WORKING_TIME_Q, TIME_DATETIME_FORMAT);
        Date dateZ = HstDateUtil.parse(String.valueOf(HstDateUtil.getCurYear()) + "-" + HstDateUtil.getCurMonth() + "-" + HstDateUtil.getCurDay() + " " + WebConstants.WORKING_TIME_Z, TIME_DATETIME_FORMAT);
        return (dateZ.getTime() - dateQ.getTime() - 3600000L) / 1000L;
    }

    private static long getWorkingTotalSecs(String dateStr, int hours) throws Exception {
        Date dateQ = HstDateUtil.parse(String.valueOf(HstDateUtil.getCurYear()) + "-" + HstDateUtil.getCurMonth() + "-" + HstDateUtil.getCurDay() + " " + WebConstants.WORKING_TIME_Q, TIME_DATETIME_FORMAT);
        Calendar calendarQ = Calendar.getInstance();
        calendarQ.setTime(dateQ);
        Date dateZ = HstDateUtil.parse(dateStr, TIME_DATETIME_FORMAT);
        Calendar calendarZ = Calendar.getInstance();
        calendarZ.setTime(dateZ);
        int hour = calendarZ.get(11) - calendarQ.get(11);
        int minutes = calendarZ.get(12) - calendarQ.get(12);
        return (hour + hours) * 60 * 60 + minutes * 60;
    }

    public static String getIntervalDateYyMMDdHhMmSs(String dateStr, int field, int amount) throws Exception {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat df = new SimpleDateFormat(TIME_DATETIME_FORMAT);
            Date date = df.parse(dateStr, new ParsePosition(0));
            gc.setTime(date);
            gc.add(field, amount);
            return df.format(gc.getTime());
        }
        catch (Exception ex) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", ex);
            throw ue;
        }
        catch (Throwable t) {
            Exception ue = new Exception("\u83b7\u53d6\u95f4\u9694\u65e5\u671f\u65f6\u4ea7\u751f\u5f02\u5e38", t);
            throw ue;
        }
    }
}
