本文實例講述了java計算時間差的方法。分享給大家供大家參考。具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/** * 計算時間差 * @param begin * @param end * @return 返回格式,"hh:mm:ss" */ public String getTimeDifference(Date begin,Date end) { long between=(end.getTime()-begin.getTime())/ 1000 ; //除以1000是為了轉換成秒 long hour=between%( 24 * 3600 )/ 3600 ; long minute=between% 3600 / 60 ; long second=between% 60 ; StringBuffer time= new StringBuffer(); if (hour!= 0 ){ time.append(hour+ ":" ); } if (time.length()!= 0 ){ time.append(String.format( "%02d:" , minute)); } else if (minute!= 0 ){ time.append(String.format( "%d:" , minute)); } if (time.length()!= 0 ){ time.append(String.format( "%02d" , second)); } else { time.append(second); } return time.toString(); } |
希望本文所述對大家的java程序設計有所幫助。