2014-09-03 25 views
-1

我有執行使用AlarmManager一些任務的方法,但是我以前來計算時間執行它, 例如,當前時間爲10:24任務必須在11:00執行那麼,如何計算10點24之間多久時間到11:00的Android,現在計算的是如何長的時間和「n」

例如 CURRENTTIME = 10:30 TargetTime = 11:00

10之間

時間長: 30和11:00的30分鐘,我就會將其轉換爲milisecond

回答

2
You can get difference between two times using Date class in java 

Date date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 
Date date2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); 

getTimeDiff(date1,date2,TimeUnit.MINUTES); 
public static long getTimeDiff(Date date1, Date date2, TimeUnit timeUnit) { 
    long diffInMillies = date2.getTime() - date1.getTime(); 
    return timeUnit.convert(diffInMillies,TimeUnit.MILLISECONDS); 
} 
+0

謝謝,樂於助人,反正我可以用日曆它做什麼? – deanrihpee 2014-09-03 07:45:39

+0

歡迎。是的,你也可以用Calendar類來做到這一點 – 2014-09-03 09:30:30

相關問題