Java Long Number to Time getHMSFromMills(long millSec)

Here you can find the source of getHMSFromMills(long millSec)

Description

get HMS From Mills

License

Open Source License

Declaration

public static String getHMSFromMills(long millSec) 

Method Source Code

//package com.java2s;
/*//  w w w . j  a va 2 s .  c o  m
 * License information at https://github.com/Caltech-IPAC/firefly/blob/master/License.txt
 */

import java.text.DecimalFormat;

public class Main {
    public static String getHMSFromMills(long millSec) {
        String retval;
        int decimal = (int) (millSec % 1000);
        int seconds = (int) (millSec / 1000);
        if (seconds > 0) {
            if (seconds < 60) {
                retval = seconds + "." + decimal + " sec";
            } else {
                String outStr = getHMS(seconds);
                retval = outStr + "." + decimal;
            }
        } else {
            retval = decimal + " ms";
        }
        return retval;
    }

    /**
     *  Get a String in HH:MM:SS format for a gievn total seconds (float)
     *  total seconds = hour*3600 + minute*60 +seconds,  Michael Nguyen
     *  @param totalSecond of seconds
     *  @return String
     */
    public static String getHMS(int totalSecond) {
        return getHMS((long) totalSecond);
    }

    /**
     *  Get a String in HH:MM:SS format for a gievn total seconds (float)
     *  total seconds = hour*3600 + minute*60 +seconds,  Michael Nguyen
     *  @param totalSecond of seconds 
     *  @return String 
     */
    public static String getHMS(long totalSecond) {
        String time = "00:00:00";
        DecimalFormat decimalFormat = new DecimalFormat("00");

        if (totalSecond > 0) {
            long hour = totalSecond / 3600;
            if (hour > 0)
                time = decimalFormat.format(hour);
            else
                time = "00";
            time += ":";
            long minute = (totalSecond - (hour * 3600)) / 60;
            if (minute > 0)
                time += decimalFormat.format(minute);
            else
                time += "00";
            time += ":";
            long second = totalSecond - (hour * 3600) - (minute * 60);
            if (second > 0)
                time += decimalFormat.format(second);
            else
                time += "00";
            return time;
        } else
            return time;

    }
}

Related

  1. getDuration(long time)
  2. getElapsedTime(long start, long end)
  3. getElapsedTime(long startTime, long finishTime)
  4. getElapsedTimeSpecificationDescription(long sizeInBytes)
  5. getExpires(long maxAge)
  6. getOldCookieDate(long time)
  7. getReadableDate(long longTime)
  8. getServerTime(long offset)
  9. getShortTimeText(Long oldTime)