2012-04-17 28 views
0

我有一個毫秒數據和CountDown類,我會以這種格式顯示時間:Days:Houres:Minutes:Seconds。 如果我做毫秒/ 1000我有總秒 如果我這樣做(毫秒/ 1000)/ 60我有總分鐘 等 但我怎樣才能以這種格式顯示倒計時:2dayes:21houres:56分鐘:00secondsAndroid:倒數計時和格式化時間

感謝

+0

我想這是你所需要的答案: http://stackoverflow.com/questions/635935/how-can-i-calculate-a-time -san-in-java-and-format-the-output – xandy 2012-04-17 08:52:42

回答

0

你應該使用日期格式或SimpleDateFormat的,看到http://developer.android.com/reference/java/text/DateFormat.html

首先,你應該將毫秒轉換爲日期。

Date date = new Date(); 
date.setTime(millis); 

然後,您可以使用DateFormat格式化您的時間戳爲人類可讀的字符串。

+0

我覺得他有什麼是時間跨度 – xandy 2012-04-17 08:51:00

+0

如何將這種格式的day/mounth/year格式的日期轉換爲毫秒? – MimmoG 2012-04-17 09:32:12

+0

我想你可以使用Date.setYear/Date.setMonth等。 – mariotaku 2012-04-17 12:46:06

1

我認爲你需要使用mod(%)運算符從你的每個部門提取餘數。

如何:

final long SEC_PER_DAY = 24 * 60 * 60; 
final long SEC_PER_HOUR = 60 * 60; 
final long SEC_PER_MIN = 60; 

public void onTick(long millis) { 
    long tot_sec = millis/1000; 
    long rem_days = tot_sec/SEC_PER_DAY; 
    long rem_hours = (tot_sec % SEC_PER_DAY)/SEC_PER_HOUR; 
    long rem_mins = ((tot_sec % SEC_PER_DAY) % SEC_PER_HOUR)/SEC_PER_MIN; 
    long rem_secs = ((tot_sec % SEC_PER_DAY) % SEC_PER_HOUR) % SEC_PER_MIN; 

    // and then format as you please... 
}