1.public class Test {

2.public void dateDiff(String startTime, String endTime, Stringformat) {

3.//按照传入的格式生成一个simpledateformate对象

4.SimpleDateFormat sd = new SimpleDateFormat(format);

5.long nd = 1000*24*60*60;//一天的毫秒数

6.long nh = 1000*60*60;//一小时的毫秒数

7.long nm = 1000*60;//一分钟的毫秒数

8.long ns = 1000;//一秒钟的毫秒数

9.long diff;

10.try {

11.//获得两个时间的毫秒时间差异

12.diff = sd.parse(endTime).getTime() -sd.parse(startTime).getTime();

13.long day = diff/nd;//计算差多少天

14.long hour = diff%nd/nh;//计算差多少小时

15.long min = diff%nd%nh/nm;//计算差多少分钟

16.long sec = diff%nd%nh%nm/ns;//计算差多少秒

17.//输出结果

18.System.out.println("时间相差:"+day+"天"+hour+"小时"+min+"分钟"+sec+"秒。");

19.} catch (ParseException e) {

20.e.printStackTrace();

21.}

22.}

23.

24.

25.public static void main(String[] args) {

26.new Test().dateDiff(newSimpleDateFormat("yyyy-MM-dd").format(new Date()),"2010-8-23", "yyyy-MM-dd");

27.}

28.}

 

PI3000源代码中的方式:在nariis.pi3000.framework.utility.DateUtil类中

**

*计算两个日期差

*@param one

*@param two

*@return

*/

publicstatic long getDiffDays(Date one, Date two)

{

Calendar sysDate = new GregorianCalendar();

sysDate.setTime(one);

 

Calendar failDate = new GregorianCalendar();

failDate.setTime(two);

 

return ((sysDate.getTimeInMillis() -failDate.getTimeInMillis()) / 86400000L);

}