Java Millisecond Convert millisToDurationString(long millis)

Here you can find the source of millisToDurationString(long millis)

Description

Given a number of milliseconds, product a string of the format
 7d6h6m.21s 

License

Apache License

Parameter

Parameter Description
millis Milliseconds in duration

Return

A duration string.

Declaration

public static String millisToDurationString(long millis) 

Method Source Code

//package com.java2s;
/*/*w  w w  . j a  va2s. c  o  m*/
 * Copyright 2007-2012 Scott C. Gray
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Given a number of milliseconds, product a string of the format
     * <pre>
     * 7d6h6m.21s
     * </pre>
     * 
     * @param millis Milliseconds in duration
     * @return A duration string.
     */
    public static String millisToDurationString(long millis) {

        StringBuilder sb = new StringBuilder();

        if (millis < 0) {

            sb.append('-');
            millis = -(millis);
        }

        long val = millis / 86400000L;
        if (val > 0L) {

            sb.append(val).append('d');
            millis %= 86400000L;
        }

        val = millis / 3600000L;
        if (sb.length() > 0 || val != 0L) {

            sb.append(val).append('h');
            millis %= 3600000L;
        }

        val = millis / 60000L;
        if (sb.length() > 0 || val != 0L) {

            sb.append(val).append('m');
            millis %= 60000L;
        }

        val = millis / 1000L;
        sb.append(val);

        millis %= 1000L;
        sb.append('.');
        append3(sb, millis);
        sb.append('s');

        return sb.toString();
    }

    private static StringBuilder append3(StringBuilder sb, long val) {

        if (val < 10) {

            sb.append("00");
        } else if (val < 100) {

            sb.append('0');
        }
        sb.append(val);
        return sb;
    }
}

Related

  1. milliSecToString(final long mS)
  2. milliSecToTime(double millis)
  3. millisToCycles(double millis, double hz)
  4. millisToDays(long millis)
  5. millisToDuration(long millis)
  6. millisToHMS(long millis)
  7. millisToHMSShort(long millis)
  8. millisToLongDHMS(long duration)
  9. millisToLongDHMS(long duration)