I want to record the time using System.currentTimeMillis() when a user begins something in my program. When he finishes, I will subtract the current System.currentTimeMillis() from the start variable, and I want to show them the time elapsed using a human readable format such as ‘XX hours, XX mins, XX seconds’ or even ‘XX mins, XX seconds’ because its not likely to take someone an hour.
What’s the best way to do this?
Use the
java.util.concurrent.TimeUnitclass:Note:
TimeUnitis part of the Java 1.5 specification, buttoMinuteswas added as of Java 1.6.To add a leading zero for values 0-9, just do:
If
TimeUnitortoMinutesare unsupported (such as on Android before API version 9), use the following equations: