先将字符串转化为Date类型

然后在调用Date.getTime();方法得到毫秒数相减即可.



/**

* 计算两个日期间的天数

*

* @param fromDate

* 起始日期

* @param toDate

* 结束日期

* @return

* @throws ParseException

*/

public static int dateDiff(String fromDate, String toDate)

throws ParseException {

int days = 0;


SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

Date from = df.parse(fromDate);

Date to = df.parse(toDate);

days = (int) Math.abs((to.getTime() - from.getTime())

 / (24 * 60 * 60 * 1000)) + 1;

 return days;

}