private int getDifference(String start, String end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String str1 = start.substring(0, 7);
String str2 = end.substring(0, 7);
Calendar bef = Calendar.getInstance();
Calendar aft = Calendar.getInstance();
try {
bef.setTime(sdf.parse(str1));
aft.setTime(sdf.parse(str2));
} catch (ParseException e) {
e.printStackTrace();
}
int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
return Math.abs(month + result);
}
java根据起始与结束时间取相差几个月
最新推荐文章于 2024-07-13 03:14:49 发布