package cn.cecep.talroad.util.effective;



import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

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

public class DateUtil {
    public static final String DATE_PATTERN_GETDAY = "dd";
    public static final String DATE_PATTERN_MONTH = "yyyy-MM";
    public static final String DATE_PATTERN_WEEK = "yyyy-ww";
    public static final String DATE_PATTERN_MONTH01 = "yyyyMM01";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String HOUR_FEN = "HH:mm";
    public static final String HOUR_TIME_PATTERN = "yyyy-MM-dd HH:00:00";
    public static final String HOUR_PATTERN = "yyyy-MM-dd HH";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String MMDD = "MM-dd";
    public static final String YYYY = "yyyy";

    public DateUtil() {
    }

    public static int weekDays(String startTime,String endTime) throws ParseException {
        Date parse = parse(startTime);
        Date parse1 = parse(endTime);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.WEEK_OF_YEAR);
        instance.setTime(parse1);
        int j = instance.get(Calendar.WEEK_OF_YEAR);
        return (j-i)*7+7;
    }
    public static List<String> weekList(String startTime,String endTime) throws ParseException {
        Date parse = parse(startTime);
        Date parse1 = parse(endTime);
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int i = instance.get(Calendar.WEEK_OF_YEAR);
        instance.setTime(parse1);
        int j = instance.get(Calendar.WEEK_OF_YEAR);
        List<String> result = new ArrayList<>();
        for (int k = i; k <= j ; k++) {
            result.add("第"+(k-1)+"周");
        }
        return result;
    }

    public static int DaysInYear(int year){
        if((year%4==0 && year%100!=0) || year%400==0 ){
            return 366;
        }else{
            return 365;
        }
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }
    public static Integer getDays(String yearTime,String startTime,String endTime) throws ParseException {
//        return format(date, "yyyy-MM-dd");
        String format1 = yearTime +"-"+format(parse(startTime), MMDD);
        String format2 = yearTime +"-"+format(parse(endTime), MMDD);
        List<String> dayList = getDayList(format1, format2);
        return dayList.size();
    }
    public static Date parse(String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = format.parse(date);
        return parse;
    }

    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        } else {
            return null;
        }
    }

    //获取日期所在月最后一天
    public static String getLastOfMonthByDate(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
        return last;
    }
    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(11);
    }

    public static Date getLastDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int lastDay = calendar.getActualMaximum(5);
        calendar.set(5, lastDay);
        return calendar.getTime();
    }



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

        return weeks[week_index];
    }



    public static Integer achieveMonth(String start, String end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();

        try {
            bef.setTime(sdf.parse(start));
            aft.setTime(sdf.parse(end));
        } catch (ParseException var7) {
            var7.printStackTrace();
        }

        int result = aft.get(2) - bef.get(2);
        int month = (aft.get(1) - bef.get(1)) * 12;
        return Math.abs(month + result) + 1;
    }

    public static Date lastDayOfMonth(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(1);
        int month = calendar.get(2) + 1;
        int lastDay = calendar.getActualMaximum(5);

        try {
            return format.parse(year + "-" + month + "-" + lastDay + " 23:59:59");
        } catch (ParseException var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static Date getStartTime(long date) {
        Date begin = new Date(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(begin);
        cal.set(5, 1);
        cal.set(11, 0);
        cal.set(13, 0);
        cal.set(12, 0);
        return cal.getTime();
    }

    public static Date getEndTime(long date) {
        Date begin = new Date(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(begin);
        int days = cal.getActualMaximum(5);
        cal.set(5, days);
        cal.set(11, 24);
        cal.set(13, 0);
        cal.set(12, 0);
        return cal.getTime();
    }


    //日期相减得天数,得出结果需要+1
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate;
        Date endDate;
        try{
            beginDate = format.parse(beginDateStr);
            endDate= format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24*60*60*1000);
        } catch (ParseException e){
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 字符串转换成日期
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)){
            return null;
        }
        Date date =null;
        try {
            DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
            date=fmt.parseLocalDateTime(strDate).toDate();
        }catch (Exception e) {
//            e.printStackTrace();

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            try {
                date=simpleDateFormat.parse(strDate);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        return date;
    }


    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(1);
    }



    public static int getYearDays(int year) {
        return (year % 4 != 0 || year % 100 == 0) && year % 400 != 0 ? 365 : 366;
    }

    private static Calendar getCalendarFormYear(int year) {
        Calendar cal = Calendar.getInstance();
        cal.set(7, 2);
        cal.set(1, year);
        return cal;
    }

    public static String getStartDayOfWeekNo(int year, int weekNo) {
        Calendar cal = getCalendarFormYear(year);
        cal.set(3, weekNo);
        return cal.get(1) + "-" + (cal.get(2) + 1) + "-" + cal.get(5);
    }

    public static String getEndDayOfWeekNo(int year, int weekNo) {
        Calendar cal = getCalendarFormYear(year);
        cal.set(3, weekNo);
        cal.add(7, 6);
        return cal.get(1) + "-" + (cal.get(2) + 1) + "-" + cal.get(5);
    }

    public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(7) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1 + k) * 7 : offset - k * 7;
        calendar.add(5, amount);
        return sdf.format(calendar.getTime());
    }

    public static String getNWeekTimeInterval(int n, SimpleDateFormat sdf) {
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 0);
        String endDate = getFromToDate(sdf, new Date(), n, 1, 0);
        return beginDate + "," + endDate;
    }

    public static String getNWeekTimeIntervalTwo(int n, SimpleDateFormat sdf) {
        String beginDate = getFromToDate(sdf, new Date(), n, 0, 1);
        String endDate = getFromToDate(sdf, new Date(), n, 1, 1);
        return beginDate + "," + endDate;
    }

    public static String getThisWeekTimeInterval(SimpleDateFormat sdf) {
        return getNWeekTimeInterval(1, sdf);
    }

    public static String getLastWeekTimeInterval(SimpleDateFormat sdf) {
        return getNWeekTimeIntervalTwo(1, sdf);
    }


    /**
     * 获取时间段天数
     * @param startDate
     * @param endDate
     * @return
     * @throws ParseException
     */
    public static int getDayDiffer(Date startDate, Date endDate) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
        long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
        return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24)+1);
    }

    /**
     * 获取月 2022-01
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getAllMonths(Date startTime, Date endTime) {
        List<String> lDate = new ArrayList();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM");
        lDate.add(sd.format(startTime));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endTime);

        while(endTime.after(calBegin.getTime())) {
            calBegin.add(5, 1);
            lDate.add(sd.format(calBegin.getTime()));
        }
        lDate = lDate.stream().distinct().collect(Collectors.toList());
        return lDate;
    }



    public static List<String> getAllDays(Date startTime, Date endTime) {
        List<String> lDate = new ArrayList();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        lDate.add(sd.format(startTime));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endTime);

        while(endTime.after(calBegin.getTime())) {
            calBegin.add(5, 1);
            lDate.add(sd.format(calBegin.getTime()));
        }

        return lDate;
    }
    public static List<String> getAllDays(String startTimes, String endTimes) throws ParseException {
        List<String> lDate = new ArrayList();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");

        Date startTime = sd.parse(startTimes);
        Date endTime = sd.parse(endTimes);
        lDate.add(sd.format(startTime).substring(5));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endTime);

        while(endTime.after(calBegin.getTime())) {
            calBegin.add(5, 1);
            lDate.add(sd.format(calBegin.getTime()).substring(5));
        }

        return lDate;
    }


    public static List<String> getAllHours(Date startTime, Date endTime, String format) {
        List<String> lDate = new ArrayList();
        SimpleDateFormat sd = new SimpleDateFormat(format);
        lDate.add(sd.format(startTime));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(startTime);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(endTime);

        while(endTime.after(calBegin.getTime())) {
            calBegin.add(11, 1);
            lDate.add(sd.format(calBegin.getTime()));
        }

        return lDate;
    }

    public static List<String> getAllMinute(Date startTime, Date endTime, String format) {
        List<String> lDate = new ArrayList();
        SimpleDateFormat sd = new SimpleDateFormat(format);

        Calendar tt = Calendar.getInstance();
        tt.setTime(startTime);
        Calendar t2 = Calendar.getInstance();
        t2.setTime(endTime);
        for (; tt.compareTo(t2) < 0; tt.add(Calendar.MINUTE, 1)) {
            if (tt.getTime().getMinutes() % 10 == 0) {
                lDate.add(sd.format(tt.getTime()));
            }
        }
        lDate.add(sd.format(t2.getTime()));
        return lDate;
    }


    public static String getToday() {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }

    public static String getYestoday() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(time);
    }

    public static Date getYestodayDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        return cal.getTime();
    }

    public static String getMonthStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(2, 0);
        cal.set(5, 1);
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(time);
    }

    public static Date getMonthStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(2, 0);
        cal.set(5, 1);
        return cal.getTime();
    }

    public static String getMonthEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(5, cal.getActualMaximum(5));
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(time);
    }

    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(4, 0);
        cal.set(7, 2);
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(time);
    }

    public static Date getWeekStartDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(4, 0);
        cal.set(7, 2);
        return cal.getTime();
    }

    public static String getWeekEnd() {
        Calendar cal = Calendar.getInstance();
        cal.set(7, cal.getActualMaximum(7));
        cal.add(7, 1);
        Date time = cal.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(time);
    }

    public static String getYearStart() {
        return (new SimpleDateFormat("yyyy")).format(new Date()) + "-01-01";
    }

    public static Date getYearStartDate() {
        Date now = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(now);
        c.set(6, 1);
        return c.getTime();
    }

    public static String getYearEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2, calendar.getActualMaximum(2));
        calendar.set(5, calendar.getActualMaximum(5));
        Date currYearLast = calendar.getTime();
        return (new SimpleDateFormat("yyyy-MM-dd")).format(currYearLast);
    }

    public static Date getYearEndDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(2, calendar.getActualMaximum(2));
        calendar.set(5, calendar.getActualMaximum(5));
        return calendar.getTime();
    }

    public static Date getOneYearStartDate(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(6, 1);
        return c.getTime();
    }

    public static Date getOneYearEndDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(2, calendar.getActualMaximum(2));
        calendar.set(5, calendar.getActualMaximum(5));
        return calendar.getTime();
    }

    //获取本月天数
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    //获取当前年总天数
    public static int getDaysOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    // 获取当前天的前（后）一天
    public static String getIntervalDay(Date date,int interval,String pattern ){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE,interval);
        return DateUtil.format(calendar.getTime(),pattern);
    }
    // 获取当前天的前（后）一个月
    public static String getIntervalMonth(Date date,int interval,String pattern ){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,interval);
        return DateUtil.format(calendar.getTime(),pattern);
    }
    // 获取当前天的前（后）一年
    public static String getIntervalYear(Date date,int interval,String pattern ){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR,interval);
        return DateUtil.format(calendar.getTime(),pattern);
    }
    // 获取当前天的前（后）一周
    public static String getIntervalWeek(Date date,int interval,String pattern ){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - (interval * 7 ));
        return DateUtil.format(calendar.getTime(),pattern);
    }

    // 判断日期格式是否正确，判断格式为 yyyy-MM-dd
    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        //指定日期格式为四位年/两位月份/两位日期，注意yyyy-MM-dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //设置lenient为false.否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }


    public static List<String> getDayList(String startDate,String endDate) throws ParseException {
        List<String> listDate = new ArrayList<String>();
//        if(StringUtils.isNotEmpty(year)){
//            Calendar startCalendar = getYearFirst(Integer.parseInt(year));
//            Calendar endCalendar = getYearLast(Integer.parseInt(year));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        long startCalendar = df.parse(startDate).getTime();
        Date endCalendar = df.parse(endDate);
        while(true){
            if(startCalendar <= endCalendar.getTime()){
                listDate.add(DateUtil.format(new Date(startCalendar)));
            }else{
                break;
            }
            startCalendar += 1000*60*60*24;
        }
//        }

        return listDate;
    }

    /**
     * 获取两个日期之间的字符串集合  yyyy-mm
     * @param startDate yyyy-mm
     * @param endDate   yyyy-mm
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthList(String startDate,String endDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(startDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(endDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }
    public static List<String> getMonthLists(String startDate,String endDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(startDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(endDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        max.add(Calendar.MONTH,1);
        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.DAY_OF_MONTH, 1);
        }

        return result;
    }
    public static List<String> getMonthListss(String startDate,String endDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(startDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(endDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
        max.add(Calendar.MONTH,1);
        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.DAY_OF_MONTH, 1);
        }

        return result;
    }

    public static Integer getMonthDays(String startDate,String endDate) throws ParseException {
        List<String> monthLists = getMonthListss(startDate, endDate);
        return monthLists.size()-1;


    }

    /**
     * 获取年份集合
     * @param startDate yyyy
     * @param endDate   yyyy
     * @return  list<yyyy>
     * @throws ParseException
     */
    public static List<String> getYearList(String startDate,String endDate) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        Integer start = Integer.parseInt(startDate);
        int end = Integer.parseInt(endDate);
        for (int i = start; i <= end ; i++) {
            result.add(i+"");
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
//        BigDecimal bigDecimal = new BigDecimal("3.1");
//        System.out.println();
//        List<String> allHours = getAllDays("2022-07-10", "2022-07-13");
//       String a = "2022-07-18";
//        String format = format(DateUtil.stringToDate(a, DATE_PATTERN), DATE_PATTERN_WEEK);
//        System.out.println(format);
//        int year = getYear(new Date());
//        System.out.println(year);

        Date s = DateUtil.stringToDate("2023-03-17 10:00:00", DateUtil.DATE_TIME_PATTERN);
        Date e = DateUtil.stringToDate("2023-03-17 16:00:00", DateUtil.DATE_TIME_PATTERN);
        System.out.println(e.getTime() - s.getTime());
    }
    /**
     * 获取时间 （常用）
     * @author heyunlong
     * @param Time
     * @return
     */
    public static Date getTime(String Time){
        try {
            if(Time.contains(".")){
                String dayStr = Time.substring(0, Time.lastIndexOf("."));
                String numStr = "0" + Time.substring(Time.lastIndexOf("."), Time.length());
                int days = Integer.parseInt(dayStr);
                double ditNumber = Double.parseDouble(numStr);
                //获取带点的时间封装
                Calendar c = Calendar.getInstance();
                c.set(1900, 0, 1);
                c.add(Calendar.DATE, days - 2);
                Date date =c.getTime();
                //时间设置值
                Calendar cc = Calendar.getInstance();
                int mills = (int) (Math.round(ditNumber * 24 * 3600));
                int hour = mills / 3600;
                int minute = (mills - hour * 3600) / 60;
                int second = mills - hour * 3600 - minute * 60;
                cc.setTime(date);
                cc.set(Calendar.HOUR_OF_DAY, hour);
                cc.set(Calendar.MINUTE, minute);
                cc.set(Calendar.SECOND, second);
                Date dateTime = cc.getTime();
                return dateTime;
            }else {
                if (Time.length()>13){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date time = format.parse(Time);
                    return time;
                }else  if (Time.length()==13){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
                    Date time = format.parse(Time);
                    return time;
                }else {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    Date time = format.parse(Time);
                    return time;
                }
            }
        }catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int getTwoDays(String yearTime,String startTime) {
        Integer year = Integer.parseInt(yearTime);
        if("1".equals(startTime)){
            return 184;
        }
        if((year%4==0 && year%100!=0) || year%400==0 ){
            return 182;
        }else{
            return 181;
        }
    }

    /**
     * 取得季度月
     *
     * @param date
     * @return
     */
    public static Date[] getSeasonDate(int nSeason) {
        Date[] season = new Date[3];

        Calendar c = Calendar.getInstance();
//        c.setTime(date);

        if (nSeason == 1) {// 第一季度
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {// 第二季度
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {// 第三季度
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {// 第四季度
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }
    /**
     * 取得月天数
     *
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 取得季度天数
     *
     * @param date
     * @return
     */
    public static int getDayOfSeason(Integer date) {
        int day = 0;
        Date[] seasonDates = getSeasonDate(date);
        for (Date date2 : seasonDates) {
            day += getDayOfMonth(date2);
        }
        return day;
    }


    /**
     * 获取时间范围内的所有的小时(yyyy-MM-dd\nHH:00)
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getAllHours(String startTimeStr,String endTimeStr, String format) throws ParseException {


        List<String> lDate = new ArrayList<String>();
        SimpleDateFormat sd = new SimpleDateFormat(format);
        Date startTime = sd.parse(startTimeStr);
        Date endTime = sd.parse(endTimeStr);


        lDate.add(sd.format(startTime).substring(10));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(startTime);

        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(endTime);
        // 测试此日期是否在指定日期之后
        while (endTime.after(calBegin.getTime()))
        {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.HOUR_OF_DAY, 1);
            lDate.add(sd.format(calBegin.getTime()).substring(10));
        }
        return lDate;
    }


    /**
     * 当月第一天
     * @param timeFlag 是否带时分秒， true是
     * @return
     */
    public static String getFirstDay(boolean timeFlag) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first);
        if(timeFlag){
            str.append(" 00:00:00");
        }
        return str.toString();
    }

    /**
     * 当月第一天
     * @return
     */
    public static String getFirstDay(Date theDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first);
        return str.toString();
    }

    public static int getCurrentMonth(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param date 日期
     * @param hours 小时数，负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }
}

