0

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

Thanks

MimmoG
  • 631
  • 3
  • 14
  • 25
  • I think this is the answer you needed: http://stackoverflow.com/questions/635935/how-can-i-calculate-a-time-span-in-java-and-format-the-output – xandy Apr 17 '12 at 08:52

2 Answers2

1

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...
}
Hephaestus
  • 1,982
  • 3
  • 27
  • 35
0

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();
date.setTime(millis);

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

mariotaku
  • 693
  • 1
  • 10
  • 26