Java Time Format formatRuntime(long runtime)

Here you can find the source of formatRuntime(long runtime)

Description

Formats a runtime in the format hh:mm:ss, to be used e.g.

License

Apache License

Parameter

Parameter Description
runtime the time to format

Return

the formatted runtime

Declaration

public static String formatRuntime(long runtime) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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  w  ww.  ja  v a  2s  . c o m*/
 *******************************************************************************/

public class Main {
    /** Day constant. */
    private static final long DAYS = 1000 * 60 * 60 * 24;
    /** Hour constant. */
    private static final long HOURS = 1000 * 60 * 60;
    /** Minute constant. */
    private static final long MINUTES = 1000 * 60;
    /** Second constant. */
    private static final long SECONDS = 1000;

    /**
     * Formats a runtime in the format hh:mm:ss, to be used e.g. in reports.<p>
     * 
     * If the runtime is greater then 24 hours, the format dd:hh:mm:ss is used.<p> 
     * 
     * @param runtime the time to format
     * 
     * @return the formatted runtime
     */
    public static String formatRuntime(long runtime) {

        long seconds = (runtime / SECONDS) % 60;
        long minutes = (runtime / MINUTES) % 60;
        long hours = (runtime / HOURS) % 24;
        long days = runtime / DAYS;
        StringBuffer strBuf = new StringBuffer();

        if (days > 0) {
            if (days < 10) {
                strBuf.append('0');
            }
            strBuf.append(days);
            strBuf.append(':');
        }

        if (hours < 10) {
            strBuf.append('0');
        }
        strBuf.append(hours);
        strBuf.append(':');

        if (minutes < 10) {
            strBuf.append('0');
        }
        strBuf.append(minutes);
        strBuf.append(':');

        if (seconds < 10) {
            strBuf.append('0');
        }
        strBuf.append(seconds);

        return strBuf.toString();
    }
}

Related

  1. formatMs(long elapsedTime)
  2. formatMusicTime(int time)
  3. formatNanosec(long timeNanosec)
  4. formatPeroid(long time)
  5. formatRemainingTime(float seccounds)
  6. formatRuntime(long time, boolean fixedlength)
  7. formatSample(final int aValue, final long aTimestamp)
  8. formatSignificantElapsedTime(final long seconds)
  9. formatStartRowKeyString(String id, String timestampStart)