日期:2014-05-18  浏览次数:20641 次

我输入两个时间,如何求出他们的差
急求

------解决方案--------------------
/**
* 计算两个日期之间的差
* 注意两个日期格式必须相同
* @param startDate 字符串开始时间
* @param endDate 字符串结束时间
* @param format 时间格式
* @return int 相差天数
* @throws ParseException
*/
public int countDays(String startDate, String endDate, String format)
throws ParseException
{
SimpleDateFormat sf = new SimpleDateFormat(format) ;
Date sDate = sf.parse(startDate) ;
Date eDate = sf.parse(endDate) ;
Calendar c = Calendar.getInstance() ;

return (int) ((eDate.getTime()-sDate.getTime())/(24*3600*1000));
}
------解决方案--------------------
2个DATE如date1和date2
计算其差:
long i = (date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24;
得到的i是以日为单位的,你可以调整上面的式子