1:返回日期之间天数
strStartDateTime String 起始时间strEndDateTime String 结束时间
public static long DateTimeDiff(String strStartDateTime,String strEndDateTime){
long intDays = 0;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date d1 = df.parse(strEndDateTime);
Date d2 = df.parse(strStartDateTime);
long diff = d1.getTime() - d2.getTime();
intDays = diff/(1000 * 60 * 60 * 24);
if(intDays == 0)
intDays = 1;
} catch (Exception e) {
e.printStackTrace();
}
return intDays;
}
2:/**
* 计算两个日期之间的月数差
* @param d1
* @param d2
* @return
*/
public static int getMonthDiff(Date d1, Date d2) {
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(d1);
c2.setTime(d2);
if(c1.getTimeInMillis() < c2.getTimeInMillis()) return 0;
int year1 = c1.get(Calendar.YEAR);
int year2 = c2.get(Calendar.YEAR);
int month1 = c1.get(Calendar.MONTH);
int month2 = c2.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
int day2 = c2.get(Calendar.DAY_OF_MONTH);
// 获取年的差值 假设 d1 = 2015-8-16 d2 = 2011-9-30
int yearInterval = year1 - year2;
// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
if(month1 < month2 || month1 == month2 && day1 < day2) yearInterval --;
// 获取月数差值
int monthInterval = (month1 + 12) - month2 ;
if(day1 < day2) monthInterval --;
monthInterval %= 12;
return yearInterval * 12 + monthInterval;
}