Calendar计算两个日期相差几个月
虾米哥
阅读:936
2021-04-01 11:17:01
评论:0
1.只考虑月,不考虑日,年相减乘以12,加月相减
public static int betweenMonthByTwoCalendar(Calendar startCalendar,Calendar endCalendar){
//判断日期大小
if(startCalendar.after(endCalendar)){
Calendar temp = startCalendar;
startCalendar = endCalendar;
endCalendar = temp;
}
int startYear = startCalendar.get(Calendar.YEAR);
int endYear = endCalendar.get(Calendar.YEAR);
int startMonth = startCalendar.get(Calendar.MONTH);
int endMonth = endCalendar.get(Calendar.MONTH);
int monthNum = (endYear - startYear)*12+(endMonth-startMonth);
return monthNum;
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。