Reputation: 331
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?
Upvotes: 2
Views: 12167
Reputation: 109247
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..
Upvotes: 2
Reputation: 31846
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
.
Upvotes: 3
Reputation: 64419
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)));
}
Upvotes: 0