2

I am trying to format the following time to hh:mm:ss:

long elapsed;
elapsed = ((System.currentTimeMillis() - startTime) / 1000);

What is the way for doing this?

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
Dori
  • 331
  • 1
  • 6
  • 18
  • Check this http://stackoverflow.com/questions/8487683/how-to-convert-date-format-in-android Hope this will help you – Ponmalar May 08 '12 at 09:32
  • Take a Look At [How to convert Milliseconds to “X mins, x seconds” in Java?][1] [1]: http://stackoverflow.com/questions/625433/how-to-convert-milliseconds-to-x-mins-x-seconds-in-java – prayagupa May 08 '12 at 09:39

3 Answers3

3

You can use Androids version of DateFormat:

DateFormat.format("hh:mm:ss", elapsed);

Note that elapsed should be in milliseconds, so you should remove the /1000.

Jave
  • 31,598
  • 14
  • 77
  • 90
2

Try this,

long elapsed;
elapsed = ((System.currentTimeMillis() - startTime) / 1000);

String display = String.format("%02d:%02d:%02d", elapsed / 3600, (elapsed % 3600) / 60, (elapsed % 60));
System.out.println(display);

And let me know what happen..

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Look at [Express a duration in term of HH:MM:SS](http://www.java2s.com/Tutorial/Java/0040__Data-Type/ExpressadurationintermofHHMMSS.htm) – user370305 May 08 '12 at 09:37
0

I suggest you use SimpleDateFormat for that? From that page an example with multiple formats:

 String[] formats = new String[] {
   "yyyy-MM-dd",
   "yyyy-MM-dd HH:mm",
   "yyyy-MM-dd HH:mmZ",
   "yyyy-MM-dd HH:mm:ss.SSSZ",
   "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
 };
 for (String format : formats) {
   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
 }
Nanne
  • 64,065
  • 16
  • 119
  • 163