package net.vtstar.zhongtong.avi.utils;

import net.vtstar.utils.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期获取帮助类
 *
 * @Author: xiadongwei
 * @Date: 2019/8/13 14:17
 */
public class DateUtil {

    /**
     * 英文简写（默认）如：2010-12-01
     */
    public static String FORMAT_SHORT = "yyyy-MM-dd";

    /**
     * 英文简写 如：20101201
     */
    public static String FORMAT_SHORT_1 = "yyyyMMddHHmmss";

    /**
     * 英文全称  如：2010-12-01 23:15:06
     */
    public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
    /**
     * 精确到毫秒的完整时间    如：yyyy-MM-dd HH:mm:ss.S
     */
    public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S";
    /**
     * 中文简写  如：2010年12月01日
     */
    public static String FORMAT_SHORT_CN = "yyyy年MM月dd日";
    /**
     * 中文简写  如：2010年12月01日
     */
    public static String FORMAT_SHORT_CN_1 = "MM月dd日";
    /**
     * 中文全称  如：2010年12月01日  23时15分06秒
     */
    public static String FORMAT_LONG_CN = "yyyy年MM月dd日  HH时mm分ss秒";
    /**
     * 精确到毫秒的完整中文时间
     */
    public static String FORMAT_FULL_CN = "yyyy年MM月dd日  HH时mm分ss秒SSS毫秒";


    public static void main(String[] args) {
        System.out.println(getTimeString());
        System.out.println("返回当天小时" + getHour(new Date()));
        System.out.println("返回当天月" + getMonth(new Date()));
        System.out.println(getYesterdayString());
        try {
            System.out.println(getYesterdayString(net.vtstar.utils.DateUtils.parse("2000-3-1", FORMAT_SHORT)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Date> twoDaysDay = getTwoDaysDay(getYesterday(), new Date());
        List<String> dayByMonth = getDayByMonth(2000, 2);
        System.out.println(getTimeString(FORMAT_SHORT_1));
        System.out.println(getEndTimeByWeek(new Date()));

    }


    /**
     * 获取当前时间
     *
     * @param dateFormat 时间格式
     */
    public static String getTimeString(String dateFormat) {
        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    /**
     * 获取指定时间
     *
     * @param dateFormat 时间格式
     */
    public static String getTimeString(Date date, String dateFormat) {
        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return df.format(calendar.getTime());
    }

    /**
     * 获取当前时间(默认格式: yyyy-MM-dd)
     */
    public static String getTimeString() {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
        Calendar calendar = Calendar.getInstance();
        return df.format(calendar.getTime());
    }

    /**
     * 获取指定时间的短时间String(默认格式: yyyy-MM-dd)
     */
    public static String getTimeString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return df.format(calendar.getTime());
    }

    /**
     * 功能描述：返回小时
     */
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 功能描述：返回当月第一天零点
     */
    public static Date getMonth(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取前一天(默认格式: yyyy-MM-dd)
     */
    public static String getYesterdayString() {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return df.format(calendar.getTime());
    }

    /**
     * 获取指定日期的前一天(默认格式: yyyy-MM-dd)
     */
    public static String getYesterdayString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return df.format(calendar.getTime());
    }

    /**
     * 获取前一天
     */
    public static Date getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 获取指定月份的每一天
     */
    public static List<String> getDayByMonth(int yearParam, int month) {
        List<String> list = new ArrayList<>();
        int day = getDays(yearParam, month);
        for (int i = 1; i <= day; i++) {
            String aDate = null;
            if (month < 10 && i < 10) {
                aDate = "0" + month + "月" + "0" + i + "日";
            }
            if (month < 10 && i >= 10) {
                aDate = "0" + month + "月" + i + "日";
            }
            if (month >= 10 && i < 10) {
                aDate = month + "月" + "0" + i + "日";
            }
            if (month >= 10 && i >= 10) {
                aDate = month + "月" + i + "日";
            }

            list.add(aDate);
        }
        return list;
    }

    /**
     * 获得两个时间之间的每一天(yyyy-MM-dd)
     */
    public static List<Date> getTwoDaysDay(Date startDate, Date endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Date> dateList = new ArrayList<>();
        String dateStart = sdf.format(startDate);
        String dateEnd = sdf.format(endDate);
        try {
            Date dateOne = sdf.parse(dateStart);
            Date dateTwo = sdf.parse(dateEnd);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(dateTwo);

            dateList.add(dateTwo);
            while (calendar.getTime().after(dateOne)) { //倒序时间,顺序after改before其他相应的改动。
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                dateList.add(calendar.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateList;
    }

    public static Date getEndTimeByWeek(Date startTime) {
        int weekDay = DateUtils.getWeekDay(startTime);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(Calendar.DATE, 7 - weekDay);

        return DateUtils.getEndOfDay(0, calendar.getTime());
    }

    public static Date getEndTimeByMonth(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND,59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();
    }

    //判断闰年
    private static boolean isLeap(int year) {
        if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0)) {
            return true;
        } else {
            return false;
        }
    }

    //返回当月天数
    private static int getDays(int year, int month) {
        int days;
        int FebDay = 28;
        if (isLeap(year)) {
            FebDay = 29;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = FebDay;
                break;
            default:
                days = 0;
                break;
        }
        return days;
    }
}
