2013-11-04 89 views
0

我想做一個簡單的時間計算,我在文本框中顯示經過的時間。問題是我的方法在返回其他東西時返回值爲零。爲了這個目的,多長時間可以投入整數?任何幫助表示讚賞。爲什麼時間計算不正確?

我的代碼:

total_hours_paycycle是一個長期擁有的259200000.值是表示這樣的調試器

 try{ 
      int total_minutes = (int) ((total_hours_paycycle/ (1000*60)) % 60); //<--Should return non-zero value, but is returning 0 
      int total_hours = (int) ((total_hours_paycycle/ (1000*60*60)) % 24); //<--Should return non-zero value, but is returning 0 
      hours_worked_converted = total_hours + "." + total_minutes; //<--returning 0.0 
      total_for_paycycle.setText("Total: "+hours_worked_converted + " hrs."); 
     }catch(Exception e){ 
      total_for_paycycle.setText("Total: - "); 
     } 
+4

你爲什麼想到第一次計算返回12? (25920000000/60000)%60 = 0. – Joel

+0

當total_hours_paycycle = 4320000時,它返回1小時12分鐘。我不明白爲什麼25920000000返回零。 – DollaBill

回答

0

在此霧氣,我建議你看一看Joda Time 。做日期數學通常會更好,並且考慮到經常被忽略的問題,例如時區,日光時間等。

它可能太多了,但是瓶子很小,它可以幫助您處理任何日期/時間你有的操作。

加上它被數百個應用程序使用,所以它的工作原理。

1

你爲什麼不使用Android的TimeUnit類,如果您正在爲API 9級及以上:

int total_minutes = (int)TimeUnit.MILLISECONDS.toMinutes(milliseconds); 
    int total_hours = (int)TimeUnit.MILLISECONDS.toHours(milliseconds);