Java Millisecond Convert milliSecToString(final long mS)

Here you can find the source of milliSecToString(final long mS)

Description

Returns the given time in milliseconds formatted as Hours:Min:Sec.mSec

License

Apache License

Parameter

Parameter Description
mS the given nanoseconds

Return

the given time in milliseconds formatted as Hours:Min:Sec.mSec

Declaration

public static String milliSecToString(final long mS) 

Method Source Code

//package com.java2s;
/*//from   ww  w  . j  ava  2 s .c  o m
 * Copyright 2015-16, Yahoo! Inc.
 * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms.
 */

public class Main {
    /**
     * Returns the given time in milliseconds formatted as Hours:Min:Sec.mSec
     * @param mS the given nanoseconds
     * @return the given time in milliseconds formatted as Hours:Min:Sec.mSec
     */
    public static String milliSecToString(final long mS) {
        final long rem_mS = (long) (mS % 1000.0);
        final long rem_sec = (long) ((mS / 1000.0) % 60.0);
        final long rem_min = (long) ((mS / 60000.0) % 60.0);
        final long hr = (long) (mS / 3600000.0);
        final String mSstr = zeroPad(Long.toString(rem_mS), 3);
        final String secStr = zeroPad(Long.toString(rem_sec), 2);
        final String minStr = zeroPad(Long.toString(rem_min), 2);
        return String.format("%d:%2s:%2s.%3s", hr, minStr, secStr, mSstr);
    }

    /**
     * Prepend the given string with zeros. If the given string is equal or greater than the given
     * field length, it will be returned without modification.
     * @param s the given string
     * @param fieldLength desired total field length including the given string
     * @return the given string prepended with zeros.
     */
    public static final String zeroPad(final String s, final int fieldLength) {
        return characterPad(s, fieldLength, '0', false);
    }

    /**
     * Prepend or postpend the given string with the given character to fill the given field length.
     * If the given string is equal or greater than the given field length, it will be returned
     * without modification.
     * @param s the given string
     * @param fieldLength the desired field length
     * @param padChar the desired pad character
     * @param postpend if true append the pacCharacters to the end of the string.
     * @return prepended or postpended given string with the given character to fill the given field
     * length.
     */
    public static final String characterPad(final String s, final int fieldLength, final char padChar,
            final boolean postpend) {
        final char[] chArr = s.toCharArray();
        final int sLen = chArr.length;
        if (sLen < fieldLength) {
            final char[] out = new char[fieldLength];
            final int blanks = fieldLength - sLen;

            if (postpend) {
                for (int i = 0; i < sLen; i++) {
                    out[i] = chArr[i];
                }
                for (int i = sLen; i < fieldLength; i++) {
                    out[i] = padChar;
                }
            } else { //prepend
                for (int i = 0; i < blanks; i++) {
                    out[i] = padChar;
                }
                for (int i = blanks; i < fieldLength; i++) {
                    out[i] = chArr[i - blanks];
                }
            }

            return String.valueOf(out);
        }
        return s;
    }
}

Related

  1. millisecondToDate(long timeMillis)
  2. millisecondToDate(String str)
  3. millisecondToDHMS(long duration)
  4. millisecsToSecs(long millisecs)
  5. milliSecsToTime(double current, double max, double buffer)
  6. milliSecToTime(double millis)
  7. millisToCycles(double millis, double hz)
  8. millisToDateString(Long Millis)
  9. millisToDays(long millis)