String endDate="20160908"; String startDate="20160708"; int result=0; int yearToMonth = (Integer.parseInt(endDate.substring(0, 4)) - Integer.parseInt(startDate.substring(0, 4))) * 12; int monthToMonth = Integer.parseInt(endDate.substring(4, 6)) - Integer.parseInt(startDate.substring(4, 6)); int monthToDate = Integer.parseInt(endDate.substring(6, 8)) - Integer.parseInt(startDate.substring(6, 8)); result=monthToMonth + yearToMonth; if (result==0 && monthToDate>0) { result=1; } else if (result>1 && monthToDate<0) { result=monthToMonth + yearToMonth-1; } System.out.println(result);
两个日期之间的相差几个月,精确到天。
最新推荐文章于 2024-06-18 14:43:27 发布