其中在數(shù)據(jù)庫(kù)中有一個(gè)字段為datetime類型,想要計(jì)算兩個(gè)日期之間過(guò)了多少天 多少小時(shí) 多少分鐘。
思路為把時(shí)間換算為毫秒(與協(xié)調(diào)世界時(shí) 1970 年 1 月 1 日午夜之間的時(shí)間差(以毫秒為單位測(cè)量)。然后利用毫秒的加減計(jì)算。
計(jì)算如下:
1
2
3
4
5
6
7
8
9
10
11
|
public static String getDays(Date date){ Calendar cal=Calendar.getInstance(); cal.setTime(date); long oldTime=cal.getTimeInMillis(); long nowTime=System.currentTimeMillis(); long days=(nowTime-oldTime)/( 1000 * 60 * 60 * 24 ); //天數(shù) long hours=((nowTime-oldTime)%( 1000 * 60 * 60 * 24 ))/( 1000 * 60 * 60 ); //小時(shí)數(shù) long minutes=(((nowTime-oldTime)%( 1000 * 60 * 60 * 24 ))%( 1000 * 60 * 60 ))/( 1000 * 60 ); //分鐘數(shù) long seconds=((((nowTime-oldTime)%( 1000 * 60 * 60 * 24 ))%( 1000 * 60 * 60 ))%( 1000 * 60 ))/ 1000 ; //秒數(shù) return days+ "天" +hours+ "小時(shí)" +minutes+ "分鐘" +seconds+ "秒" ; } |
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/su20145104009/article/details/64441266