整个java类如下:
package com.cfne.cuckoo.motor.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
public class DateUtils {
/**
* 两个月份相减
* @param startMonth
* @param endMonth
* @return
*/
public static int monthNumber48(String startMonth,String endMonth){
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy年MM月");
YearMonth before = YearMonth.parse(startMonth,sdf);
YearMonth after = YearMonth.parse(endMonth,sdf);
int result = before.getMonthValue() - after.getMonthValue();
int month = (before.getYear() - after.getYear())*12;
return month+result;
}
public static void main(String[] args) throws ParseException {
System.out.println(Math.abs(monthNumber48("2018年11月","2017年02月")));
//System.out.println(beforeMonth48("2017-11"));
}
}