Java TimeUnit Usage formatTimeInMilliSeconds(long duration)

Here you can find the source of formatTimeInMilliSeconds(long duration)

Description

Formats the time into mill

License

Open Source License

Parameter

Parameter Description
duration a parameter

Declaration

public static String formatTimeInMilliSeconds(long duration) 

Method Source Code


//package com.java2s;
import java.util.concurrent.TimeUnit;

public class Main {
    /**/*from  w w  w.  j av  a  2s . co m*/
     * Formats the time into mill
     * @param duration
     * @return
     */
    public static String formatTimeInMilliSeconds(long duration) {
        try {
            StringBuffer durationInString = new StringBuffer("");
            //TODO: add hours here as well
            long minutes = TimeUnit.MILLISECONDS.toSeconds(duration) / 60;
            long seconds = TimeUnit.MILLISECONDS.toSeconds(duration - minutes * 60 * 1000);
            long milliseconds = duration - (minutes * 60 * 1000) - seconds * 1000;
            if (minutes > 0) {
                durationInString.append(minutes).append(" min ");
            }
            if (seconds > 0) {
                durationInString.append(seconds).append(" sec ");
            }
            if (milliseconds > 0) {
                durationInString.append(milliseconds).append(" msec");
            }
            return durationInString.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "-1";
        }
    }
}

Related

  1. formatTime(long time)
  2. formatTime(long time)
  3. formatTime(long timeInNanoseconds)
  4. formatTime(String format, double nanoseconds)
  5. formatTimeDuration(long duration)
  6. formatUptime(long startTime)
  7. formatUptime(long uptimeInMs)
  8. generateRandomDateTimeForLogEvent(Date laterThan)
  9. getAge(Date birthday)