Java Millisecond Format formatMillis(long millis)

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

Description

Returns a formatted string using the pattern hh:mm:ss.

License

Apache License

Declaration

public static String formatMillis(long millis) 

Method Source Code

//package com.java2s;
/* 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./*from  www.j a  v  a2s. c o  m*/
 */

public class Main {
    /**
     * Returns a formatted string using the pattern hh:mm:ss. The hours are
     * omitted if they are zero, the minutes are padded with a '0' character
     * if they are less than 10.
     */
    public static String formatMillis(long millis) {
        int hours = (int) (millis / (1000 * 60 * 60));
        int minutes = (int) (millis / (1000 * 60)) % 60;
        int seconds = (int) (millis / 1000) % 60;
        StringBuilder sb = new StringBuilder();
        if (hours > 0) {
            sb.append(hours);
            sb.append(':');
        }
        if (minutes < 10 && hours > 0) {
            sb.append(0);
        }
        sb.append(minutes);
        sb.append(':');
        if (seconds < 10) {
            sb.append(0);
        }
        sb.append(seconds);
        return sb.toString();
    }
}

Related

  1. formatMillis(long duration)
  2. formatMillis(long millis)
  3. formatMillis(long millis)
  4. formatMillis(Long millis)
  5. formatMillis(long millis)
  6. formatMillis(long t)
  7. formatMillis(long time)
  8. formatMillis(long time, boolean showMillis)
  9. formatMillis(long timeMillis)