|  |  |  | 
|---|
|  |  |  | package com.doumee.core.utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.math.RoundingMode; | 
|---|
|  |  |  | import java.sql.Timestamp; | 
|---|
|  |  |  | import java.text.DateFormat; | 
|---|
|  |  |  | import java.text.DateFormatSymbols; | 
|---|
|  |  |  | import java.text.ParseException; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.time.LocalDate; | 
|---|
|  |  |  | import java.time.Period; | 
|---|
|  |  |  | import java.time.ZoneId; | 
|---|
|  |  |  | import java.util.Calendar; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.Enumeration; | 
|---|
|  |  |  | 
|---|
|  |  |  | calo.setTime(oldDate); | 
|---|
|  |  |  | caln.setTime(newDate); | 
|---|
|  |  |  | if(type==0||type==1){ | 
|---|
|  |  |  | return    calculateDaysBetweenDates(caln, calo) + 1; | 
|---|
|  |  |  | return    calculateDaysBetweenDates(caln, calo)  + 1 ; | 
|---|
|  |  |  | }else if(type==2){ | 
|---|
|  |  |  | return    calculateMonthsBetweenDates(caln, calo) + 1; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int day = caln.get(Calendar.DATE); | 
|---|
|  |  |  | if(day == 1){ | 
|---|
|  |  |  | return    calculateMonthsBetweenDates(caln, calo) + 1; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return    calculateMonthsBetweenDates(caln, calo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else if(type==3){ | 
|---|
|  |  |  | return    calculateYearsBetweenDates(caln, calo) + 1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | return sdfLongTimePlus.parse(sdfLongTimePlus.format(date)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | System.out.println(RandomStringUtils.randomNumeric(3)); | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | ex.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * | 
|---|
|  |  |  | 
|---|
|  |  |  | return nowDateString; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @ author zhangyong @ 获取当月的第一天,2009-05-01 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | return calendar.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取指定日期的下个月第一天 | 
|---|
|  |  |  | * @param date | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Date getNextMonthFirst(Date date){ | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(date); | 
|---|
|  |  |  | calendar.add(Calendar.MONTH, 1); // 将日历的月份增加1 | 
|---|
|  |  |  | calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为下个月的第一天 | 
|---|
|  |  |  | return calendar.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static String afterDateToStr(Integer days){ | 
|---|
|  |  |  | Date date = new Date(); | 
|---|
|  |  |  | 
|---|
|  |  |  | cal.add(Calendar.MINUTE,minutes); | 
|---|
|  |  |  | return cal.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 计算多少自然年/月/日后的日期 | 
|---|
|  |  |  | * @param startDate 开始日期 | 
|---|
|  |  |  | * @param type  0=日;1=月;2=年; | 
|---|
|  |  |  | * @param cycle 周期  X月 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Date afterDateByType(Date startDate,Integer type,Integer cycle){ | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(startDate); | 
|---|
|  |  |  | if(type==0){ | 
|---|
|  |  |  | calendar.add(Calendar.DATE, cycle); | 
|---|
|  |  |  | }else if(type==1){ | 
|---|
|  |  |  | calendar.add(Calendar.MONTH, cycle); | 
|---|
|  |  |  | }else if(type == 2) { | 
|---|
|  |  |  | calendar.add(Calendar.YEAR, cycle); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return calendar.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static Integer monthDays(Date date){ | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(date); | 
|---|
|  |  |  | // 获取当前年份、月份(注意月份从0开始计算) | 
|---|
|  |  |  | int year = calendar.get(Calendar.YEAR); | 
|---|
|  |  |  | int month = calendar.get(Calendar.MONTH) + 1; | 
|---|
|  |  |  | // 根据年份和月份判断闰年情况 | 
|---|
|  |  |  | boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 根据不同月份设定每个月的天数 | 
|---|
|  |  |  | switch (month) { | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | if (isLeapYear) { | 
|---|
|  |  |  | return 29; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return 28; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | case 4: | 
|---|
|  |  |  | case 6: | 
|---|
|  |  |  | case 9: | 
|---|
|  |  |  | case 11: | 
|---|
|  |  |  | return 30; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | return 31; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 拼接日期数据 | 
|---|
|  |  |  | * @param date | 
|---|
|  |  |  | * @param optType   1:每日开始 2:每日结束 3:昨日结束 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Date getMontageDate(Date date ,Integer optType) { | 
|---|
|  |  |  | String strShortDate = DateUtil.dateToString(date,"yyyy-MM-dd"); | 
|---|
|  |  |  | if(optType.equals(1)){ | 
|---|
|  |  |  | return DateUtil.StringToDate(strShortDate +" 00:00:00"); | 
|---|
|  |  |  | }else if(optType.equals(2)){ | 
|---|
|  |  |  | return DateUtil.StringToDate(strShortDate +" 23:59:59"); | 
|---|
|  |  |  | }else if(optType.equals(3)){ | 
|---|
|  |  |  | strShortDate = DateUtil.dateToString(DateUtil.getDateBetween(date,-1),"yyyy-MM-dd"); | 
|---|
|  |  |  | return DateUtil.StringToDate(strShortDate +" 23:59:59"); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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" | 
|---|
|  |  |  | //                ) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        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); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /*        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") | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取相差的月数 | 
|---|
|  |  |  | * @param sDate | 
|---|
|  |  |  | * @param eDate | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Integer getDifferenceMonths(Date sDate ,Date eDate) { | 
|---|
|  |  |  | LocalDate startDate = sDate.toInstant() | 
|---|
|  |  |  | .atZone(ZoneId.systemDefault()) | 
|---|
|  |  |  | .toLocalDate(); | 
|---|
|  |  |  | LocalDate endDate = eDate.toInstant() | 
|---|
|  |  |  | .atZone(ZoneId.systemDefault()) | 
|---|
|  |  |  | .toLocalDate(); | 
|---|
|  |  |  | //        LocalDate startDate= LocalDate.of(2021,2,28); | 
|---|
|  |  |  | //        LocalDate endDate =LocalDate.of(2022,4,5); | 
|---|
|  |  |  | Period period = Period.between(endDate,startDate); | 
|---|
|  |  |  | int months = period.getYears()* 12 + period.getMonths(); | 
|---|
|  |  |  | return months + 1; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|