|  |  | 
 |  |  |     /** | 
 |  |  |      * 得出两个日期之间的间隔小时 | 
 |  |  |      * | 
 |  |  |      * @param fromDate | 
 |  |  |      * @param fromDate 靠前的时间 | 
 |  |  |      *            格式为yyyy-MM-dd | 
 |  |  |      * @param toDate | 
 |  |  |      * @param toDate  靠后的时间 | 
 |  |  |      *            格式为yyyy-MM-dd | 
 |  |  |      *                DateUtil.getBetweenHours(DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 10:46:43"), | 
 |  |  |      *                 DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 07:41:43")) 返回 -2小时 | 
 |  |  |      * @return int | 
 |  |  |      */ | 
 |  |  |     public static long getBetweenHours(Date fromDate, Date toDate) { | 
 |  |  | 
 |  |  |         try { | 
 |  |  |             m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数 | 
 |  |  |             m_intervalday = m_intervalday / 1000 / 60 / 60;// 计算所得的天数 | 
 |  |  |  | 
 |  |  |             return m_intervalday + 1; | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             return Integer.MIN_VALUE; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static long getBetweenMin(Date fromDate, Date toDate) { | 
 |  |  |         long m_intervalday = 0;// 初始化时间间隔的值为0 | 
 |  |  |         // 使用的时间格式为yyyy-MM-dd | 
 |  |  |         try { | 
 |  |  |             m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数 | 
 |  |  |             m_intervalday = m_intervalday / 1000 / 60 ;// 计算所得的天数 | 
 |  |  |  | 
 |  |  |             return m_intervalday + 1; | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             return Integer.MIN_VALUE; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public static long getBetweenDay(Date fromDate, Date toDate) { | 
 |  |  |         long m_intervalday = 0;// 初始化时间间隔的值为0 | 
 |  |  |         // 使用的时间格式为yyyy-MM-dd | 
 |  |  |         try { | 
 |  |  |             m_intervalday = toDate.getTime() - fromDate.getTime();// 计算所得为微秒数 | 
 |  |  |             m_intervalday = m_intervalday / 1000 / 60 / 60 / 24 ;// 计算所得的天数 | 
 |  |  |  | 
 |  |  |             return m_intervalday + 1; | 
 |  |  |         } catch (Exception e) { | 
 |  |  | 
 |  |  |             throw e; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * Descrption:取得当前日期,格式为:yyyy-MM-dd HH:mm:ss | 
 |  |  | 
 |  |  |      * @return 转换得到的日期 | 
 |  |  |      */ | 
 |  |  |     @SuppressWarnings("unchecked") | 
 |  |  |     public static Date stringToDate(String strDate, String oracleFormat) { | 
 |  |  |         if (strDate == null) | 
 |  |  |             return null; | 
 |  |  |         Hashtable<Integer, String> h = new Hashtable<Integer, String>(); | 
 |  |  |         String javaFormat = new String(); | 
 |  |  |         String s = oracleFormat.toLowerCase(); | 
 |  |  |         if (s.indexOf("yyyy") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("yyyy")), "yyyy"); | 
 |  |  |         else if (s.indexOf("yy") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("yy")), "yy"); | 
 |  |  |         if (s.indexOf("mm") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("mm")), "MM"); | 
 |  |  |  | 
 |  |  |         if (s.indexOf("dd") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("dd")), "dd"); | 
 |  |  |         if (s.indexOf("hh24") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("hh24")), "HH"); | 
 |  |  |         if (s.indexOf("mi") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("mi")), "mm"); | 
 |  |  |         if (s.indexOf("ss") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("ss")), "ss"); | 
 |  |  |  | 
 |  |  |         int intStart = 0; | 
 |  |  |         while (s.indexOf("-", intStart) != -1) { | 
 |  |  |             intStart = s.indexOf("-", intStart); | 
 |  |  |             h.put(new Integer(intStart), "-"); | 
 |  |  |             intStart++; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         intStart = 0; | 
 |  |  |         while (s.indexOf("/", intStart) != -1) { | 
 |  |  |             intStart = s.indexOf("/", intStart); | 
 |  |  |             h.put(new Integer(intStart), "/"); | 
 |  |  |             intStart++; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         intStart = 0; | 
 |  |  |         while (s.indexOf(" ", intStart) != -1) { | 
 |  |  |             intStart = s.indexOf(" ", intStart); | 
 |  |  |             h.put(new Integer(intStart), " "); | 
 |  |  |             intStart++; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         intStart = 0; | 
 |  |  |         while (s.indexOf(":", intStart) != -1) { | 
 |  |  |             intStart = s.indexOf(":", intStart); | 
 |  |  |             h.put(new Integer(intStart), ":"); | 
 |  |  |             intStart++; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (s.indexOf("年") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("年")), "年"); | 
 |  |  |         if (s.indexOf("月") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("月")), "月"); | 
 |  |  |         if (s.indexOf("日") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("日")), "日"); | 
 |  |  |         if (s.indexOf("时") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("时")), "时"); | 
 |  |  |         if (s.indexOf("分") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("分")), "分"); | 
 |  |  |         if (s.indexOf("秒") != -1) | 
 |  |  |             h.put(new Integer(s.indexOf("秒")), "秒"); | 
 |  |  |  | 
 |  |  |         int i = 0; | 
 |  |  |         while (h.size() != 0) { | 
 |  |  |             Enumeration e = h.keys(); | 
 |  |  |             int n = 0; | 
 |  |  |             while (e.hasMoreElements()) { | 
 |  |  |                 i = ((Integer) e.nextElement()).intValue(); | 
 |  |  |                 if (i >= n) | 
 |  |  |                     n = i; | 
 |  |  |             } | 
 |  |  |             String temp = (String) h.get(new Integer(n)); | 
 |  |  |             h.remove(new Integer(n)); | 
 |  |  |  | 
 |  |  |             javaFormat = temp + javaFormat; | 
 |  |  |         } | 
 |  |  |         SimpleDateFormat df = new SimpleDateFormat(javaFormat); | 
 |  |  |  | 
 |  |  |         Date myDate = new Date(); | 
 |  |  |         try { | 
 |  |  |             myDate = df.parse(strDate); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             // e.printStackTrace(); | 
 |  |  |             return null; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return myDate; | 
 |  |  |     } | 
 |  |  | //    public static Date stringToDate(String strDate, String oracleFormat) { | 
 |  |  | //        if (strDate == null) | 
 |  |  | //            return null; | 
 |  |  | //        Hashtable<Integer, String> h = new Hashtable<Integer, String>(); | 
 |  |  | //        String javaFormat = new String(); | 
 |  |  | //        String s = oracleFormat.toLowerCase(); | 
 |  |  | //        if (s.indexOf("yyyy") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("yyyy")), "yyyy"); | 
 |  |  | //        else if (s.indexOf("yy") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("yy")), "yy"); | 
 |  |  | //        if (s.indexOf("mm") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("mm")), "MM"); | 
 |  |  | // | 
 |  |  | //        if (s.indexOf("dd") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("dd")), "dd"); | 
 |  |  | //        if (s.indexOf("hh24") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("hh24")), "HH"); | 
 |  |  | //        if (s.indexOf("mi") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("mi")), "mm"); | 
 |  |  | //        if (s.indexOf("ss") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("ss")), "ss"); | 
 |  |  | // | 
 |  |  | //        int intStart = 0; | 
 |  |  | //        while (s.indexOf("-", intStart) != -1) { | 
 |  |  | //            intStart = s.indexOf("-", intStart); | 
 |  |  | //            h.put(new Integer(intStart), "-"); | 
 |  |  | //            intStart++; | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        intStart = 0; | 
 |  |  | //        while (s.indexOf("/", intStart) != -1) { | 
 |  |  | //            intStart = s.indexOf("/", intStart); | 
 |  |  | //            h.put(new Integer(intStart), "/"); | 
 |  |  | //            intStart++; | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        intStart = 0; | 
 |  |  | //        while (s.indexOf(" ", intStart) != -1) { | 
 |  |  | //            intStart = s.indexOf(" ", intStart); | 
 |  |  | //            h.put(new Integer(intStart), " "); | 
 |  |  | //            intStart++; | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        intStart = 0; | 
 |  |  | //        while (s.indexOf(":", intStart) != -1) { | 
 |  |  | //            intStart = s.indexOf(":", intStart); | 
 |  |  | //            h.put(new Integer(intStart), ":"); | 
 |  |  | //            intStart++; | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        if (s.indexOf("年") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("年")), "年"); | 
 |  |  | //        if (s.indexOf("月") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("月")), "月"); | 
 |  |  | //        if (s.indexOf("日") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("日")), "日"); | 
 |  |  | //        if (s.indexOf("时") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("时")), "时"); | 
 |  |  | //        if (s.indexOf("分") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("分")), "分"); | 
 |  |  | //        if (s.indexOf("秒") != -1) | 
 |  |  | //            h.put(new Integer(s.indexOf("秒")), "秒"); | 
 |  |  | // | 
 |  |  | //        int i = 0; | 
 |  |  | //        while (h.size() != 0) { | 
 |  |  | //            Enumeration e = h.keys(); | 
 |  |  | //            int n = 0; | 
 |  |  | //            while (e.hasMoreElements()) { | 
 |  |  | //                i = ((Integer) e.nextElement()).intValue(); | 
 |  |  | //                if (i >= n) | 
 |  |  | //                    n = i; | 
 |  |  | //            } | 
 |  |  | //            String temp = (String) h.get(new Integer(n)); | 
 |  |  | //            h.remove(new Integer(n)); | 
 |  |  | // | 
 |  |  | //            javaFormat = temp + javaFormat; | 
 |  |  | //        } | 
 |  |  | //        SimpleDateFormat df = new SimpleDateFormat(javaFormat); | 
 |  |  | // | 
 |  |  | //        Date myDate = new Date(); | 
 |  |  | //        try { | 
 |  |  | //            myDate = df.parse(strDate); | 
 |  |  | //        } catch (Exception e) { | 
 |  |  | //            // e.printStackTrace(); | 
 |  |  | //            return null; | 
 |  |  | //        } | 
 |  |  | // | 
 |  |  | //        return myDate; | 
 |  |  | //    } | 
 |  |  |  | 
 |  |  |     public static Date StringToDate(String DATE1) { | 
 |  |  |         DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); | 
 |  |  | 
 |  |  |         return DateToString(date, "yyyyMMddHHmmss"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String getLongDateTime(Date date) { | 
 |  |  |         return DateToString(date, "yyyy-MM-dd HH:mm:ss"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String DateToString(Date date, String dateStyle) { | 
 |  |  |         String dateString = null; | 
 |  |  |         if (dateStyle != null) { | 
 |  |  | 
 |  |  |         return cal.getTime(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取某个时间X分钟后的时间 | 
 |  |  |      * @param minutes | 
 |  |  |      * @param date | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static Date afterMinutesByDate(Integer minutes,Date date){ | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         cal.setTime(date); | 
 |  |  |         cal.add(Calendar.MINUTE,minutes); | 
 |  |  |         return cal.getTime(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static Date afterDayByDate(Integer days,Date date){ | 
 |  |  |         Calendar cal = Calendar.getInstance(); | 
 |  |  |         cal.setTime(date); | 
 |  |  |         cal.add(Calendar.DATE,days); | 
 |  |  |         return cal.getTime(); | 
 |  |  |     } | 
 |  |  |     /** | 
 |  |  |      * 计算多少自然年/月/日后的日期 | 
 |  |  |      * @param startDate 开始日期 | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public static void main(String[] args) { | 
 |  |  | //       System.out.println(DateUtil.DateToStr(DateUtil.afterDateByType( | 
 |  |  | //               DateUtil.stringToDate("2024-02-01","yyyy-MM-dd") | 
 |  |  | //               ,1,1),"yyyy-MM-dd HH:mm:ss")); | 
 |  |  |  | 
 |  |  | //        System.out.println( DateUtil.daysBetweenDates(DateUtil.stringToDate("2024-02-29","yyyy-MM-dd"), | 
 |  |  | //                DateUtil.stringToDate("2024-02-01","yyyy-MM-dd")) | 
 |  |  | //        ); | 
 |  |  | // | 
 |  |  | //        System.out.println(DateUtil.calculateBetween(DateUtil.StringToDate("2024-02-29 23:59:59"),DateUtil.StringToDate("2024-02-01 00:00:00"),0)); | 
 |  |  |  | 
 |  |  |  | 
 |  |  | //        System.out.println( | 
 |  |  | //                DateUtil.DateToStr( | 
 |  |  | // | 
 |  |  | //                DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),1) ,"yyyy-MM-dd HH:mm:ss" | 
 |  |  | //                ) | 
 |  |  | //        ); | 
 |  |  | // | 
 |  |  | // | 
 |  |  | // | 
 |  |  | //        System.out.println( | 
 |  |  | // | 
 |  |  | //                DateUtil.DateToStr( | 
 |  |  | // | 
 |  |  | //                        DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),2) ,"yyyy-MM-dd HH:mm:ss" | 
 |  |  | //                ) | 
 |  |  | // | 
 |  |  | //        ); | 
 |  |  | // | 
 |  |  | //        System.out.println( | 
 |  |  | // | 
 |  |  | //                DateUtil.DateToStr( | 
 |  |  | // | 
 |  |  | //                        DateUtil.getMontageDate(DateUtil.StringToDate("2024-02-29 21:59:59"),3) ,"yyyy-MM-dd HH:mm:ss" | 
 |  |  | //                ) | 
 |  |  |  | 
 |  |  |     public static void main(String[] args) throws Exception{ | 
 |  |  | //        System.out.println(DateUtil.DateToStr( | 
 |  |  | //                DateUtil.afterDateByType(new Date(),1,-1),"yyyy-MM") | 
 |  |  | //        ); | 
 |  |  |  | 
 |  |  | //        System.out.println( | 
 |  |  | //                new BigDecimal(16).divide(new BigDecimal(15.5),0, RoundingMode.CEILING) | 
 |  |  | //        ); | 
 |  |  |         LocalDate startDate = DateUtil.StringToDate("2021-02-28 23:59:59").toInstant() | 
 |  |  |                 .atZone(ZoneId.systemDefault()) | 
 |  |  |                 .toLocalDate(); | 
 |  |  |         LocalDate endDate = DateUtil.StringToDate("2022-04-05 23:59:59").toInstant() | 
 |  |  |                 .atZone(ZoneId.systemDefault()) | 
 |  |  |                 .toLocalDate(); | 
 |  |  | //        LocalDate startDate= LocalDate.of(2021,2,28); | 
 |  |  | //        LocalDate endDate =LocalDate.of(2022,4,5); | 
 |  |  |         Period period = Period.between(startDate,endDate); | 
 |  |  |         int months = period.getYears()* 12 + period.getMonths(); | 
 |  |  |         System.out.println("Months between the two dates: " + months); | 
 |  |  |         System.out.println(DateUtil.getBetweenHours(DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 10:46:43"), | 
 |  |  |                 DateUtil.fromStringToDate("yyyy-MM-dd HH:mm:ss","2025-05-26 07:41:43"))); | 
 |  |  |         ; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | /*        Calendar calo = Calendar.getInstance(); | 
 |  |  |         Calendar caln = Calendar.getInstance(); // ,"yyyy-MM-dd HH:mm:ss" | 
 |  |  |         caln.setTime(DateUtil.StringToDate("2024-03-06 23:59:59")); | 
 |  |  |         calo.setTime(DateUtil.StringToDate("2024-02-05 00:00:00")); | 
 |  |  |         System.out.println( DateUtil.calculateMonthsBetweenDates(calo,caln));*/ | 
 |  |  |  | 
 |  |  | //        System.out.println( | 
 |  |  | //                DateUtil.DateToStr( | 
 |  |  | //                        DateUtil.getNextMonthFirst(new Date()) | 
 |  |  | //                        ,"yyyy-MM-dd HH:mm:ss") | 
 |  |  | // | 
 |  |  | //        ); | 
 |  |  | //        System.out.println( | 
 |  |  | //                DateUtil.DateToStr( | 
 |  |  | //                        DateUtil.getNextMonthFirst( | 
 |  |  | //                                DateUtil.StringToDate("2024-12-29 21:59:59") | 
 |  |  | //                        ) | 
 |  |  | //                        ,"yyyy-MM-dd HH:mm:ss") | 
 |  |  | // | 
 |  |  | //        ); | 
 |  |  |  | 
 |  |  | //        System.out.println(DateUtil.getLongDateTime(new Date()));; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** |