/**
* 计算两个日期月份差,不足一月按0月算,
* 日期没有前后顺序,时间不参与计算
* getMonthSub 2019.12.22 00:00:00 2019.12.23 00:00:00 0
* getMonthSub 2019.11.23 00:00:00 2019.12.22 00:00:00 0
* getMonthSub 2019.12.23 00:00:00 2020.12.22 00:00:00 11
* getMonthSub 2018.11.02 00:00:00 2019.02.03 00:00:00 3
* getMonthSub 2018.11.02 00:00:00 2019.02.01 00:00:00 2
*
* getMonthSub 2019.12.23 00:00:00 2019.12.22 00:00:00 0
* getMonthSub 2019.12.22 00:00:00 2019.11.23 00:00:00 0
* getMonthSub 2020.12.22 00:00:00 2019.12.23 00:00:00 11
* getMonthSub 2019.02.03 00:00:00 2018.11.02 00:00:00 3
* getMonthSub 2019.02.01 00:00:00 2018.11.02 00:00:00 2
* @param time1 时间1
* @param time2 时间2
* @param format 时间格式
* @return
*/
public static int getMonthSub(String time1,String time2,String format)throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar before = Calendar.getInstance();
Calendar after = Calendar.getInstance();
Date beforeDate = sdf.parse(time1);
Date afterDate = sdf.parse(time2);
if(beforeDate.compareTo(afterDate)>0){
before.setTime(afterDate);
after.setTime(beforeDate);
}else if(beforeDate.compareTo(afterDate)<0){
before.setTime(beforeDate);
after.setTime(afterDate);
}else{
return 0;
}
int surplus = after.get(Calendar.DATE) - before.get(Calendar.DATE);
int result = after.get(Calendar.MONTH) - before.get(Calendar.MONTH);
int month = (after.get(Calendar.YEAR) - before.get(Calendar.YEAR)) * 12;
surplus = surplus < 0 ? -1 : 0;
return Math.abs(month + result) + surplus;
}
public static void main(String[] args){
try {
System.out.println("getMonthSub 2019.12.22 00:00:00 2019.12.23 00:00:00 " + getMonthSub("2019.12.22 00:00:00", "2019.12.23 00:00:00", "yyyy.MM.dd HH:mm:ss"));//0
System.out.println("getMonthSub 2019.11.23 00:00:00 2019.12.22 00:00:00 " + getMonthSub("2019.11.23 00:00:00", "2019.12.22 00:00:00", "yyyy.MM.dd HH:mm:ss"));//0
System.out.println("getMonthSub 2019.12.23 00:00:00 2020.12.22 00:00:00 " + getMonthSub("2019.12.23 00:00:00", "2020.12.22 00:00:00", "yyyy.MM.dd HH:mm:ss"));//11
System.out.println("getMonthSub 2018.11.02 00:00:00 2019.02.03 00:00:00 " + getMonthSub("2018.11.02 00:00:00", "2019.02.03 00:00:00", "yyyy.MM.dd HH:mm:ss"));//3
System.out.println("getMonthSub 2018.11.02 00:00:00 2019.02.01 00:00:00 " + getMonthSub("2018.11.02 00:00:00", "2019.02.01 00:00:00", "yyyy.MM.dd HH:mm:ss"));//2
System.out.println();
System.out.println("getMonthSub 2019.12.23 00:00:00 2019.12.22 00:00:00 " + getMonthSub("2019.12.23 00:00:00", "2019.12.22 00:00:00", "yyyy.MM.dd HH:mm:ss"));//0
System.out.println("getMonthSub 2019.12.22 00:00:00 2019.11.23 00:00:00 " + getMonthSub("2019.12.22 00:00:00", "2019.11.23 00:00:00", "yyyy.MM.dd HH:mm:ss"));//0
System.out.println("getMonthSub 2020.12.22 00:00:00 2019.12.23 00:00:00 " + getMonthSub("2020.12.22 00:00:00", "2019.12.23 00:00:00", "yyyy.MM.dd HH:mm:ss"));//11
System.out.println("getMonthSub 2019.02.03 00:00:00 2018.11.02 00:00:00 " + getMonthSub("2019.02.03 00:00:00", "2018.11.02 00:00:00", "yyyy.MM.dd HH:mm:ss"));//3
System.out.println("getMonthSub 2019.02.01 00:00:00 2018.11.02 00:00:00 " + getMonthSub("2019.02.01 00:00:00", "2018.11.02 00:00:00", "yyyy.MM.dd HH:mm:ss"));//2
}catch (Exception e){
e.printStackTrace();
}
}