Take the 2-minute tour ×

I have a data in milliseconds and with CountDown class I would to display the time in this format : Days : Houres : Minutes : Seconds. If I do milliseconds / 1000 I have the total second If I do (milliseconds / 1000) / 60 I have the total minutes Etc but how can I display a countdown in this format : 2dayes : 21houres : 56 minutes : 00seconds


share|improve this question
I think this is the answer you needed: stackoverflow.com/questions/635935/… –  xandy Apr 17 '12 at 8:52
add comment

2 Answers

up vote 0 down vote accepted

you should use DateFormat or SimpleDateFormat, see http://developer.android.com/reference/java/text/DateFormat.html

First, you should convert your milliseconds to Date.

Date date = new Date();

Then you could use DateFormat to format your timestamp to human-readable string.

share|improve this answer
I think what he have is a time span –  xandy Apr 17 '12 at 8:51
How can I convert a date in this format day/mounth/year to milliseconds ? –  MimmoG Apr 17 '12 at 9:32
I guess you could use Date.setYear/Date.setMonth etc. –  mariotaku Apr 17 '12 at 12:46
add comment

I think you need to extract remainders from each of your divisions, using the mod (%) operator.

How about this:

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...
share|improve this answer
add comment

Your Answer


