package com.inspect.base.core.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String yyyy = "yyyy";
    public static String yyyy_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String HHmmss = "HHmmss";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String yyyyMMdd2 = "yyyy-MM-dd";
    public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    public static final String yyyyMMddHHmmss2 = "yyyy-MM-dd HH:mm:ss";
    private static String[] parsePatterns = new String[]{"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

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

    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(String format, Date date) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static final Date dateTime(String format, String ts) {
        try {
            return (new SimpleDateFormat(format)).parse(ts);
        } catch (ParseException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        } else {
            try {
                return parseDate(str.toString(), parsePatterns);
            } catch (ParseException var2) {
                return null;
            }
        }
    }

    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 86400000L;
        long nh = 3600000L;
        long nm = 60000L;
        long diff = endDate.getTime() - nowDate.getTime();
        long day = diff / nd;
        long hour = diff % nd / nh;
        long min = diff % nd % nh / nm;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static Date toDate(LocalDateTime temporalAccessor) {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    public static Date toDate(LocalDate temporalAccessor) {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

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

    public static Date theDayBeforeStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        calendar.add(5, -1);
        Date date_start = calendar.getTime();
        return date_start;
    }

    public static Date theDayBeforeEndTime(Date date) {
        Calendar calendar_end = Calendar.getInstance();
        calendar_end.setTime(date);
        calendar_end.set(11, 23);
        calendar_end.set(12, 59);
        calendar_end.set(13, 59);
        calendar_end.set(14, 999);
        calendar_end.add(5, -1);
        Date date_end = calendar_end.getTime();
        return date_end;
    }

    public static Date theDayTimeByAddHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, hour);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        calendar.add(5, 0);
        Date date_time = calendar.getTime();
        return date_time;
    }

    public static String format(final String pattern, final Date date) {
        try {
            return new SimpleDateFormat(pattern).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static int toInt(final String pattern, final Date date) {
        try {
            return Integer.parseInt(format(pattern, date));
        } catch (Exception e) {
            return 0;
        }
    }

    public static Date parse(final String pattern, final String time) {
        try {
            return new SimpleDateFormat(pattern).parse(time);
        } catch (Exception e) {
            return new Date(0);
        }
    }

    public static int getYearInt() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getMonthInt() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    public static int getDayInt() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    public static int getHourInt() {
        return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinuteInt() {
        return Calendar.getInstance().get(Calendar.MINUTE);
    }

    public static String getDayEven() {
        int day = Calendar.getInstance().get(5);
        String dayStr = "" + day;
        if (day < 10) {
            dayStr = "0" + dayStr;
        }
        return dayStr;
    }

    public static String getMonthEven() {
        int month = Calendar.getInstance().get(2) + 1;
        String res = "" + month;
        if (month < 10) {
            res = "0" + res;
        }
        return res;
    }

    public static String getYearEven() {
        return "" + Calendar.getInstance().get(Calendar.YEAR);
    }

    public static String getDayOfWeek(Date date) {
        String[] weekDays = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }

        return weekDays[w];
    }

    public static void main(String[] args) {
        theDayBeforeStartTime(new Date());
        theDayBeforeEndTime(new Date());
        theDayStartTime(new Date());
        Date now = new Date();
        System.out.println(now);
        theDayTimeByAddHour(getNowDate(), 24);
        int interval = 24;
        Date dayBeforeStartTime = theDayBeforeStartTime(new Date());

        int hour;
        for (int i = 0; i < interval; ++i) {
            hour = i * 1 + 1;
            Date startTime = theDayTimeByAddHour(dayBeforeStartTime, i * 1);
            System.out.println("startTime::" + startTime);
            Date endTime = theDayTimeByAddHour(dayBeforeStartTime, hour);
            System.out.println("  endTime::" + endTime);
        }

        Calendar now1 = Calendar.getInstance();
        hour = now1.get(11);
        Double a = Double.valueOf((double) (hour / 1));
        int b = a.intValue();
        System.out.println("时: " + now1.get(11));
        System.out.println("时: " + b);
    }
}
