package com.ym.mec.education.utils;

import java.math.BigDecimal;
import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @program: mec
 * @description: 时间工具类
 * @author: xw
 * @create: 2019-09-26 21:22
 */
public class DateUtil {

    private static final String DATE_FORMAT_CHINESE = "MM月dd日";
    public static final String TIME_FORMAT = "HH:mm";
    public static final String TIME_FORMAT_MIN = "HH:mm:ss";
    public static final String DATE_FORMAT = "MM-dd";
    public static final String DATE_FORMAT_HOUR = "MM-dd HH:mm";
    public static final String DATE_FORMAT_MIN = "yyyy年MM月dd日 HH:mm";
    public static final String DATE_FORMAT_EXTRA = "yyyy/MM/dd";
    public static final String DATE_FORMAT_DATE = "yyyy-MM-dd HH:mm:ss";
    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<DateFormat>();

    public static DateFormat getDateFormat(String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        threadLocal.set(df);
        return df;
    }

    public static String date2String(Date date) {
        DateFormat dateFormat = getDateFormat(DATE_FORMAT_CHINESE);
        String format = dateFormat.format(date);
        if (format.startsWith(BigDecimal.ZERO.toString())) {
            format = format.substring(1);
        }
        return format;
    }

    public static String date2String(Date date, String dateFormatPattern) {
        DateFormat dateFormat = getDateFormat(dateFormatPattern);
        return dateFormat.format(date);
    }

    public static String time2String(Time time) {
        String format = time.toLocalTime().format(DateTimeFormatter.ofPattern(TIME_FORMAT));
        return format;
    }

    public static String time2String(Time time, String dateFormatPattern) {
        String format = time.toLocalTime().format(DateTimeFormatter.ofPattern(dateFormatPattern));
        return format;
    }

    /**
     * 返回分钟差
     *
     * @param time1
     * @param time2
     * @return
     */
    public static Integer timeDifference(Time time1, Time time2) {
        LocalTime localTime1 = time1.toLocalTime();
        LocalTime localTime2 = time2.toLocalTime();
        Integer hour = localTime2.getHour() - localTime1.getHour();
        Integer minute = localTime2.getMinute() - localTime1.getMinute();
        return hour * 60 + minute;
    }

    public static String date2Week(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
            default:
                return "";
        }
    }

    public static String date2Week2(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int i = calendar.get(Calendar.DAY_OF_WEEK);
        switch (i) {
            case 1:
                return "周日";
            case 2:
                return "周一";
            case 3:
                return "周二";
            case 4:
                return "周三";
            case 5:
                return "周四";
            case 6:
                return "周五";
            case 7:
                return "周六";
            default:
                return "";
        }
    }

    /**
     * 获取下个月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfNextMonth(Date date) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MONTH, 1);
            return calendar.getTime();
        } catch (Exception e) {
        }
        return null;
    }


    public static Integer subMin(Date startTime, Date endTime) {

        if (startTime != null && endTime != null) {
            long from = startTime.getTime();
            long to = endTime.getTime();
            Integer hours = (int) ((to - from) / (1000 * 60 * 60));

            return hours;
        }

        return null;
    }
}
