package com.gyee.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : DateUtils
 * @Author : xieshengjie
 * @Date: 2021/10/5 17:35
 * @Description : 日期工具
 */
public class DateUtils {


    private static final String format = "yyyy-MM-dd";
    private static final String format1 = "yyyy-MM-dd HH:mm:ss";
    private static final String format2 = "MM/dd/yyyy HH:mm:ss";
    private static final String format3 = "yyyy-MM";
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_PATTERN = "yyyy-MM-dd";

    // 第一次调用get将返回null

    private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>();

    // 获取线程的变量副本，如果不覆盖initialValue，第一次get返回null，故需要初始化一个SimpleDateFormat，并set到threadLocal中
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    public static SimpleDateFormat getFormat() {

        SimpleDateFormat df = (SimpleDateFormat) threadLocal.get();

        if (df == null) {
            df = new SimpleDateFormat(format);
            threadLocal.set(df);
        }

        return df;

    }

    public static SimpleDateFormat getFormat1() {

        SimpleDateFormat df1 = (SimpleDateFormat) threadLocal.get();

        if (df1 == null) {
            df1 = new SimpleDateFormat(format1);
            threadLocal.set(df1);
        }

        return df1;

    }

    public static SimpleDateFormat getFormat2() {

        SimpleDateFormat df2 = (SimpleDateFormat) threadLocal.get();

        if (df2 == null) {
            df2 = new SimpleDateFormat(format2);
            threadLocal.set(df2);
        }

        return df2;

    }

    public static SimpleDateFormat getFormat3() {

        SimpleDateFormat df3 = (SimpleDateFormat) threadLocal.get();

        if (df3 == null) {
            df3 = new SimpleDateFormat(format3);
            threadLocal.set(df3);
        }

        return df3;

    }

    private DateUtils() {
    }

    /**
     * 获取系统日期（无时分秒毫秒）
     *
     * @return
     */
    public static Date today() {
        return truncate(now());
    }

    /**
     * 获取系统时间
     *
     * @return
     */
    public static Date now() {
        return new Date();
    }

    /**
     * 根据年月日生成日期对象
     *
     * @param y
     * @param m
     * @param d
     * @return
     */
    public static Date cons(int y, int m, int d) {
        Calendar cal = Calendar.getInstance();
        cal.set(y, m, d, 0, 0, 0);
        return cal.getTime();
    }

    public static String toDate(Date date) {
        return getFormat1().format(date);
    }

    public static String toDate2(Date date) {
        return getFormat2().format(date);
    }

    public static String toDate3(Date date) {
        return getFormat3().format(date);
    }

    public static String toDate1(Date date) {
        return getFormat().format(date);
    }

    /**
     * 根据年月日时分秒生成日期对象
     *
     * @param y
     * @param m
     * @param d
     * @param h
     * @param mi
     * @param s
     * @return
     */
    public static Date cons(int y, int m, int d, int h, int mi, int s) {
        Calendar cal = Calendar.getInstance();
        cal.set(y, m, d, h, mi, s);
        return cal.getTime();
    }

    /**
     * 将指定时间转化为 Calendar
     *
     * @param date
     * @return
     */
    public static Calendar getCal(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    /**
     * 获取当年年份
     *
     * @return
     */
    public static Integer getCurrentYear() {
        Calendar date = Calendar.getInstance();
        int year = date.get(Calendar.YEAR);
        return year;

    }

    /**
     * 将时间的时分秒毫秒字段去掉
     *
     * @param date
     * @return
     */
    public static Date truncate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 去掉日期中日及下级字段
     *
     * @param date
     * @return
     */
    public static Date truncDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 去掉日期中的月及下级字段
     *
     * @param date
     * @return
     */
    public static Date truncMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 在指定时间上加指定的天数
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDays(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 在指定的时间上加指定的月数
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonths(Date date, int month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }

    /**
     * 在指定的时间上加指定的月数
     *
     * @param date
     * @param year
     * @return
     */
    public static Date addYears(Date date, int year) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 在指定时间上加指定的小时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date addHours(Date date, int hour) {
        return new Date(date.getTime() + hour * 3600 * 1000);
    }

    /**
     * 在指定时间上加指定的分钟
     *
     * @param date
     * @param m
     * @return
     */
    public static Date addMinutes(Date date, int m) {
        return new Date(date.getTime() + m * 60 * 1000);
    }

    /**
     * 在指定时间上加指定的秒
     *
     * @param date
     * @param s
     * @return
     */
    public static Date addSeconds(Date date, int s) {
        return new Date(date.getTime() + s * 1000);
    }

    /**
     * 计算两个时间之间差的天数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int daysDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 24 * 1000));
    }

    /**
     * 计算两个时间之间差的小时数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int hoursDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 60 * 1000));
    }

    public static double hoursDiff1(Date d1, Date d2) {
        return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000));
    }

    public static double hoursDiff2(Date d1, Date d2) {
        return Math.abs((d1.getTime() - d2.getTime())) / (double) (60 * 60 * 1000);
    }

    /**
     * 计算两个时间之间差的分钟数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int minutesDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
    }

    /**
     * 计算两个时间之间差的分钟数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static double minutesDiff2(Date d1, Date d2) {
        return Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (60 * 1000));
    }

    /**
     * 计算两个时间之间差的毫秒数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static long millisecondDiff(Date d1, Date d2) {
        return Math.abs(d1.getTime() - d2.getTime());
    }

    /**
     * 计算两个时间之间差的秒数(取整后)
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int secondsDiff(Date d1, Date d2) {
        return (int) Math.floor(Math.abs((d1.getTime() - d2.getTime())) / (1000));
    }

    /**
     * 计算两个时间之间的月差
     *
     * @param d1
     * @param d2
     * @return
     */
    public static int monthsDiff(Date d1, Date d2) {
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(d1);
        cal2.setTime(d2);

        return (int) Math.abs((cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH));

    }

    /**
     * 获取当前周的周一的日期
     *
     * @param date 传入当前日期
     * @return
     */
    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获取指定日期的月初
     */
    public static Date getFirstAndLastDayOfMonth(Date date) throws Exception {

        Calendar cale = Calendar.getInstance();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        cale.setTime(date);

        cale.add(Calendar.MONTH, 0);

        cale.set(Calendar.DAY_OF_MONTH, 1);

        String firstDayOfMonth = formatter.format(cale.getTime()); // 当月第一天 2019-02-01

        return parseDate(firstDayOfMonth);

    }

    /**
     * 获取季度开始
     *
     * @param date
     * @return
     */
    public static Date getSeasonStartDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month / 3 * 3);
        calendar.set(Calendar.DATE, 1);
        return calendar.getTime();
    }

    public static Date getYearBegin(Date time) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        //设置月和日都为1，即为开始时间（注：月份是从0开始;日中0表示上个月最后一天，1表示本月开始第一天）
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        //将小时置为0
        cal.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟置为0
        cal.set(Calendar.MINUTE, 0);
        //将秒置为0
        cal.set(Calendar.SECOND, 0);
        //将毫秒置为0
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();

    }


    /**
     * 获取指定时间的天数
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        return cd.get(Calendar.DAY_OF_MONTH);
    }

    public static int getCurrentMonthLastDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    public static int getMonthDays(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.set(DateUtils.getYear(date), DateUtils.getMonth(date), DateUtils.getDay(date));
        int dayst = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return dayst;
    }

    /**
     * 获取某年第一天日期
     *
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取当前日期
     *
     * @param
     * @return Date
     */
    public static Date getCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        return date;
    }


    /**
     * 获取某日期的当月第一天
     *
     * @param date
     * @return Date
     */
    public static Date getMonthFirst(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        int month = getMonth(date);
        int year = getYear(date);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }


    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static String getCurrtenFirstDay() {

        Calendar c = Calendar.getInstance();
        // c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return getFormat().format(c.getTime());
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static String getCurrtenLastDay() {

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getFormat().format(ca.getTime());
    }

    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static Date getCurrtenFirstDate() {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 1);
        return c.getTime();
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static Date getCurrtenLastDate() {

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime();
    }

    public static Date parseDate(String date) {
        try {
            return getFormat().parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDate1(String date) {
        try {
            return getFormat1().parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDate2(String date) {
        try {
            return getFormat2().parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseDate3(String date) {
        try {
            return getFormat3().parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parseLongToDate(long time) {
        return new Date(time);
    }


    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        //tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.add(Calendar.DATE, +1);
        tempEnd.setTime(end);

        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    /**
     * 获取上月最后一天
     *
     * @return
     */
    public static Date getYestmonthLastday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();

    }


    /**
     * 转换Edna时间格式为标准格式
     *
     * @param pointTime
     * @return
     */
    public static String convertEdnaTime2(String pointTime, Boolean isNoSec) {
        StringBuffer sb = new StringBuffer();
        String[] dt = pointTime.split(" ");
        String[] ymd = dt[0].split("-");
        String[] hms = dt[1].split(":");
        sb.append(ymd[0]).append("-");
        if (ymd[1].length() == 1) {
            sb.append("0").append(ymd[1]);
        } else {
            sb.append(ymd[1]);
        }
        if (ymd[2].length() == 1) {
            sb.append("-").append("0").append(ymd[2]);
        } else {
            sb.append("-").append(ymd[2]);
        }
        if (hms[0].length() == 1) {
            sb.append(" ").append("0").append(hms[0]);
        } else {
            sb.append(" ").append(hms[0]);
        }
        if (hms[1].length() == 1) {
            sb.append(":").append("0").append(hms[1]);
        } else {
            sb.append(":").append(hms[1]);
        }

        if (isNoSec) {
            sb.append(":").append("00");
        } else {
            if (hms[2].length() == 1) {
                sb.append(":").append("0").append(hms[2]);
            } else {
                sb.append(":").append(hms[2]);
            }
        }

        return sb.toString();
    }

    /**
     * 转换Edna时间格式为标准格式
     *
     * @param pointTime
     * @return
     */
    public static String convertEdnaTime(String pointTime, Boolean isNoSec) {
        String date = getFormat().format(new Date());
        StringBuffer sb = new StringBuffer();
        String[] dt = pointTime.split(" ");
        String[] ymd = dt[0].split("/");
        String[] hms = dt[1].split(":");
        if (ymd[2].length() == 2) {
            sb.append(date.substring(0, 2)).append(ymd[2]).append("-");
        }
        if (ymd[0].length() == 1) {
            sb.append("0").append(ymd[0]);
        } else {
            sb.append(ymd[0]);
        }
        if (ymd[1].length() == 1) {
            sb.append("-").append("0").append(ymd[1]);
        } else {
            sb.append("-").append(ymd[1]);
        }
        if (hms[0].length() == 1) {
            sb.append(" ").append("0").append(hms[0]);
        } else {
            sb.append(" ").append(hms[0]);
        }
        if (hms[1].length() == 1) {
            sb.append(":").append("0").append(hms[1]);
        } else {
            sb.append(":").append(hms[1]);
        }

        if (isNoSec) {
            sb.append(":").append("00");
        } else {
            if (hms[2].length() == 1) {
                sb.append(":").append("0").append(hms[2]);
            } else {
                sb.append(":").append(hms[2]);
            }
        }

        return sb.toString();
    }

    /**
     * 获取两个日期之间的所有日期(字符串格式, 按天计算)
     *
     * @param startTime String 开始时间 yyyy-MM-dd
     * @param endTime   String 结束时间 yyyy-MM-dd
     * @return
     */
    public static List<String> getBetweenDays(String startTime, String endTime) throws ParseException {

        //1、定义转换格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Date start = df.parse(startTime);
        Date end = df.parse(endTime);

        List<String> result = new ArrayList<String>();

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);

        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        result.add(sdf.format(start));
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    public static String convertEdnaTime(String pointTime) {
        return convertEdnaTime2(pointTime, false);
    }

    /**
     * 获取昨天凌晨时间
     *
     * @return
     */
    public static Date getYesterdayStart() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString = format.format(date);

        time.append(dateString).append(" ").append("00:00:00");

        return sdf.parse(time.toString());

    }

    /**
     * 时间减去一年
     *
     * @return
     */
    public static Date subOneYear(Date date) throws ParseException {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间
        return calendar.getTime();

    }

    /**
     * 获取昨天最后时间
     *
     * @return
     */
    public static Date getYesterdayEnd() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);

        date = calendar.getTime();

        SimpleDateFormat format = getDateFormat("yyyy-MM-dd");

        StringBuffer time = new StringBuffer();

        String dateString = format.format(date);

        time.append(dateString).append(" ").append("23:59:59");

        return sdf.parse(time.toString());
    }

    /**
     * 获取SimpleDateFormat
     *
     * @param parttern 日期格式
     * @return SimpleDateFormat对象
     * @throws RuntimeException 异常：非法日期格式
     */
    public static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
        return new SimpleDateFormat(parttern);
    }

    /**
     * 获取昨天日期
     *
     * @param format
     * @return
     */
    public static String getYesterdayStr(String format) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat(format).format(cal.getTime());
    }

    /**
     * z获取前一天
     *
     * @return
     */
    public static Date getYestday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();

    }


    // 获得某天最大时间 2020-02-19 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获得某天最小时间 2020-02-17 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.getMonthDays(DateUtils.today()));
    }

    /**
     * 获取连个日期间的所有日期
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获得指定时间的月数
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        return cd.get(Calendar.MONTH) + 1;
    }

    /**
     * 用于从日期对象中获取月份的字符串形式
     *
     * @param date
     * @return
     */
    // 定义一个方法，用于从日期对象中获取月份的字符串形式
    public static Integer getMonthFromDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        return (month);
    }

    /**
     * 获得指定时间的年数
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        return cd.get(Calendar.YEAR);
    }

    /**
     * 获取某日期的当月最后一天
     *
     * @param date
     * @return Date
     */
    public static Date getMonthLast(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        int month = getMonth(date);
        int year = getYear(date);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 获取当天0点
     *
     * @return
     */
    public static Date getSamedayZero() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        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();
    }

    /**
     * 获取前一天23点59分59秒
     *
     * @return
     */
    public static Date geteleven() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getSamedayZero(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate(date));
        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();
    }

    /**
     * 获取当月第一天0点
     *
     * @return
     */
    public static Date getMonthFirstZero() {
        Calendar calendar = Calendar.getInstance();
        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();
    }

    public static Date getMonthFirstZero(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate(date));
        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();
    }

    /**
     * 获取当年第一天0点
     *
     * @return
     */
    public static Date getYearFirstZero() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, year);
        calendar.set(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();
    }

    public static Date getYearFirstZero(String date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate(date));
        int year = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, year);
        calendar.set(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();
    }

    /**
     * 获取当前日期小时数
     *
     * @param
     * @return
     */
    public static int getCurrentHour() {
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.HOUR_OF_DAY);
        return i;
    }

    /**
     * 判断是否为当天日期
     *
     * @param inputJudgeDate
     * @return
     */
    public static boolean isToday(Date inputJudgeDate) {
        boolean flag = false;
        // 获取当前系统时间
        long longDate = System.currentTimeMillis();
        Date nowDate = new Date(longDate);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(nowDate);
        String subDate = format.substring(0, 10);
        // 定义每天的24h时间范围
        String beginTime = subDate + " 00:00:00";
        String endTime = subDate + " 23:59:59";
        Date paseBeginTime = null;
        Date paseEndTime = null;
        try {
            paseBeginTime = dateFormat.parse(beginTime);
            paseEndTime = dateFormat.parse(endTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (inputJudgeDate.after(paseBeginTime) && inputJudgeDate.before(paseEndTime)) {
            flag = true;
        }
        return flag;
    }

    /**
     * 判断日期是否为本月1号
     */
    public static boolean isFirstDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
        if (cal.get(Calendar.DAY_OF_MONTH) == 2) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断日期是否为当年第一天
     */
    public static boolean isFirstDayOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH) + 1;
        if (month == 1 && isFirstDayOfMonth(date)) {
            return true;
        } else {
            return false;
        }
    }
}
