|  |  |  | 
|---|
|  |  |  | 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.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; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LocalDate startDate= LocalDate.of(2021,2,28); | 
|---|
|  |  |  | LocalDate endDate =LocalDate.of(2022,4,5); | 
|---|
|  |  |  | //        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); | 
|---|
|  |  |  | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取相差的月数 | 
|---|
|  |  |  | * @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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|