使用Period对象,进行计算两个时间之间,间隔的月数
Date nowYearMonth = new SimpleDateFormat("yyyy-MM").parse(DateUtils.parseDateToStr("yyyy-MM", new Date())); String suspenseAccountDate = rule.getSuspenseAccountDate(); String dateType = rule.getDateType(); String suspenseMonth = rule.getSuspenseMonth(); Instant nowInstant = nowYearMonth.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate now = nowInstant.atZone(zoneId).toLocalDate(); Date insureMonth = new SimpleDateFormat("yyyy-MM").parse(vo.getInsureMonth()); Instant insureMonthInstant = insureMonth.toInstant(); LocalDate insureMonthDate = insureMonthInstant.atZone(zoneId).toLocalDate(); //对比现在时间和业务费用所属月 int distance = Period.between(now, insureMonthDate).getMonths();