|  |  |  | 
|---|
|  |  |  | *                                以年为单位相差为:6年 | 
|---|
|  |  |  | *                                以月为单位相差为:73个月 | 
|---|
|  |  |  | *                                以日为单位相差为:2220天 | 
|---|
|  |  |  | * @param fromDate | 
|---|
|  |  |  | * @param toDateOrigin | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static DateCompare dayCompare(Date fromDate,Date toDateOrigin,Date freeStart,Date freeEnd){ | 
|---|
|  |  |  | public static DateCompare dayCompare(Date fromDateOrigin,Date toDateOrigin,Date freeStart,Date freeEnd){ | 
|---|
|  |  |  | //开始时间往后延伸,除去有效时期 | 
|---|
|  |  |  | fromDate = DateUtil.addDaysToDate(fromDate,getIntersectingDays(fromDate,DateUtil.addDaysToDate(toDateOrigin,1),freeStart,DateUtil.addDaysToDate(freeEnd,1))); | 
|---|
|  |  |  | Date fromDate = DateUtil.addDaysToDate(fromDateOrigin,getIntersectingDays(fromDateOrigin,DateUtil.addDaysToDate(toDateOrigin,1), | 
|---|
|  |  |  | freeStart, | 
|---|
|  |  |  | Objects.isNull(freeEnd)?null:DateUtil.addDaysToDate(freeEnd,1)) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if(toDateOrigin.getTime()<  fromDate.getTime()){ | 
|---|
|  |  |  | return DateCompare.builder().day(0).month(0).year(0).yearFloat(new BigDecimal(0)).monthFloat(new BigDecimal(0)).build(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | BigDecimal yearFloat = new BigDecimal(year) ; | 
|---|
|  |  |  | BigDecimal monthFloat = new BigDecimal(month) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int yearDays = day - (DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate)+1); | 
|---|
|  |  |  | int yearDays = day - (DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate)); | 
|---|
|  |  |  | if(yearDays!=0){ | 
|---|
|  |  |  | if(yearDays <0){ | 
|---|
|  |  |  | year = year-1; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | yearFloat = yearFloat.add(new BigDecimal(1.0*yearDays).divide(new BigDecimal(365.0), 15,RoundingMode.HALF_UP)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int monthDays = day - (DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate)+1) ; | 
|---|
|  |  |  | int monthDays = day - (DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate)) ; | 
|---|
|  |  |  | if(monthDays!=0){ | 
|---|
|  |  |  | if(monthDays <0){ | 
|---|
|  |  |  | month = month-1; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | DateCompare dateCompare =   DateCompare.monthYearCompare(DateUtil.getDateFromString("2025-03-02 00:00:00"),DateUtil.getDateByString("2025-06-02 00:00:00") ); | 
|---|
|  |  |  | DateCompare dateCompare =   DateCompare.monthYearCompare(DateUtil.getDateFromString("2025-06-03 00:00:00"),DateUtil.getDateByString("2025-09-02 00:00:00") ); | 
|---|
|  |  |  | System.out.println(dateCompare.day); | 
|---|
|  |  |  | System.out.println(dateCompare.monthDays); | 
|---|
|  |  |  | System.out.println(dateCompare.month); | 
|---|
|  |  |  | 
|---|
|  |  |  | BigDecimal yearFloat = new BigDecimal(year) ; | 
|---|
|  |  |  | BigDecimal monthFloat = new BigDecimal(month) ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int yearDays = day - (DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate)+1); | 
|---|
|  |  |  | int yearDays = day - (DateUtil.daysBetweenDates(DateUtil.addYearToDate(fromDate,year),fromDate)); | 
|---|
|  |  |  | if(yearDays!=0){ | 
|---|
|  |  |  | if(yearDays <0){ | 
|---|
|  |  |  | year = year-1; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | yearFloat = yearFloat.add(new BigDecimal(1.0*yearDays).divide(new BigDecimal(365.0), 15,RoundingMode.HALF_UP)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int monthDays = day - (DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate)+1) ; | 
|---|
|  |  |  | int monthDays = day - (DateUtil.daysBetweenDates(DateUtil.addMonthToDate(fromDate,month),fromDate)) ; | 
|---|
|  |  |  | if(monthDays!=0){ | 
|---|
|  |  |  | if(monthDays <0){ | 
|---|
|  |  |  | month = month-1; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static int getIntersectingDays(Date start1, Date end1, Date start2, Date end2) { | 
|---|
|  |  |  | if(Objects.isNull(start2)||Objects.isNull(end2)){ | 
|---|
|  |  |  | return 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Date earlierStart = DateUtil.daysBetweenDates(start1,start2)>0? start1 : start2; | 
|---|
|  |  |  | Date laterEnd =  DateUtil.daysBetweenDates(end2,end1)>0 ? end1 : end2; | 
|---|
|  |  |  |  | 
|---|