Android Long Format getTimeFormattedFromMillis(long millis)

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

Description

Returns a formatted time string in the form of (hh:)mm:ss generated from milliseconds

License

Apache License

Parameter

Parameter Description
millis a parameter

Declaration

public static String getTimeFormattedFromMillis(long millis) 

Method Source Code

/*//w w w . j av a 2s .  co  m
 * Copyright (C) 2014 Peter M?senthin <peter.moesenthin@gmail.com>
 *
 * 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.
 */

import android.content.Context;
import java.util.Calendar;

public class Main{
    /**
     * Returns a formatted time string in the form of (hh:)mm:ss generated from milliseconds
     * @param millis
     * @return
     */
    public static String getTimeFormattedFromMillis(long millis) {
        long duration = millis / 1000;
        long h = duration / 3600;
        long m = (duration - h * 3600) / 60;
        long s = duration - (h * 3600 + m * 60);
        String durationString = "";
        if (h != 0) {
            durationString += StringUtil.getZeroPaddedString(h) + ":";
        }
        durationString += StringUtil.getZeroPaddedString(m) + ":";
        durationString += StringUtil.getZeroPaddedString(s);
        return durationString;
    }
    public static String getZeroPaddedString(int number) {
        if (number >= 10)
            return String.valueOf(number);
        else
            return "0" + String.valueOf(number);
    }
    public static String getZeroPaddedString(long number) {
        if (number >= 10)
            return String.valueOf(number);
        else
            return "0" + String.valueOf(number);
    }
}

Related

  1. convert2Percent(long readsize, long totalsize)
  2. formatSize(long value)
  3. formatSpaceSize(long file_size)
  4. formatPercent(long divisor, long dividend)
  5. formatSize(long value)
  6. formatFileSize(long length)
  7. ReadableByteCount(long bytes)
  8. getDisplayable(long numBytes)
  9. formatByte(long l)