Android Second Format getTimeFormattedFromSeconds(int seconds)

Here you can find the source of getTimeFormattedFromSeconds(int seconds)

Description

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

License

Apache License

Parameter

Parameter Description
seconds a parameter

Declaration

public static String getTimeFormattedFromSeconds(int seconds) 

Method Source Code

/*//  w ww  . j  a  va2  s. c  o 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 seconds
     * @param seconds
     * @return
     */
    public static String getTimeFormattedFromSeconds(int seconds) {
        return getTimeFormattedFromMillis(seconds * 1000);
    }
    /**
     * 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. formatTime(int second)
  2. formatTime(int second)
  3. formatIntoHHMMSS(int secsIn)
  4. formatElapsedTime(long elapsedSeconds)
  5. formatElapsedTime(StringBuilder recycle, long elapsedSeconds)
  6. formatElapsedTime(StringBuilder recycle, String format, long minutes, long seconds)
  7. currentSeconds()