Java Millisecond Format formatMillis(long millis)

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

Description

format Millis

License

Apache License

Declaration

public static String formatMillis(long millis) 

Method Source Code

//package com.java2s;
/*//  w  ww.  j  a va 2  s  . c om
 * Copyright 2012 JBoss Inc
 *
 * 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 {
    public static String formatMillis(long millis) {
        if (millis == 0L) {
            return "0";
        }
        StringBuilder millisString = new StringBuilder();
        long leftover = millis;
        if (leftover >= 86400000L) {
            millisString.append(leftover / 86400000L).append("d ");
            leftover %= 86400000L;
        }
        if (leftover >= 3600000L) {
            millisString.append(leftover / 3600000L).append("h ");
            leftover %= 3600000L;
        }
        if (leftover >= 60000L) {
            millisString.append(leftover / 60000L).append("m ");
            leftover %= 60000L;
        }
        if (leftover >= 1000L) {
            millisString.append(leftover / 1000L).append("s ");
            leftover %= 1000L;
        }
        if (leftover >= 1L) {
            millisString.append(leftover).append("ms ");
        }
        return millisString.deleteCharAt(millisString.length() - 1).toString();
    }
}

Related

  1. formatElapsedTime(long millis)
  2. formateTime(long milliseconds)
  3. formatMillis(int millis, int digits)
  4. formatMillis(long duration)
  5. formatMillis(long millis)
  6. formatMillis(Long millis)
  7. formatMillis(long millis)
  8. formatMillis(long millis)
  9. formatMillis(long t)