2015-06-03 72 views
0

我有一個CountDownTimer,我想顯示未來兩週的計數,應該從13d:00h:00:59s開始倒計時。然而,下面的一段代碼給了我一個我不明白的結果。Android的CountDownTimer

354h:16m:59s

這裏是我的代碼。

public class CounterClass1 extends CountDownTimer { 

    public CounterClass1(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 

    diffDays.setText("League finishes in: " +  
     String.format("%02dd:%02dh:%02dm:%02ds", 

     TimeUnit.MILLISECONDS.toDays(millisUntilFinished), 

     TimeUnit.MILLISECONDS.toHours(millisUntilFinished) - 
     TimeUnit.MILLISECONDS.toDays(

     TimeUnit.DAYS.toDays(millisUntilFinished)), 
     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - 
     TimeUnit.HOURS.toMinutes(

     TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 

     TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
     TimeUnit.MINUTES.toSeconds(

     TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 
    } 

    @Override 
    public void onFinish() { 
     //diffDays.setText("Completed"); 
    } 
} 

任何建議真的會有所幫助。

謝謝, Theo。

+0

'DAYS.toDays'讓你通過了好些日子代表的天數。 (即它返回輸入) – njzk2

回答

0

你只是做轉換錯誤似乎..

int days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished); 
int hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished) 
     - TimeUnit.DAYS.toHours(days); 
int minutes = TimeUnit.MILLISECOND.toMinutes(millisUntilFinished) 
     - (TimeUnit.DAYS.toMinutes(days) + TimeUnit.HOURS.toMinutes(hours)); 
int seconds = TimeUnit.MILLISECOND.toSeconds(millisUntilFinished) 
     - (TimeUnit.DAYS.toSeconds(days) + TimeUnit.HOURS.toSeconds(hours) + TimeUnit.MINUTES.toSeconds(minutes)); 

String.format("%02dd:%02dh:%02dm:%02ds", days, hours, minutes, seconds)); 
+0

謝謝你的回覆。我試圖改變這一點。 – Theo