Here you can find the source of formatRuntime(long runtime)
Parameter | Description |
---|---|
runtime | the time to format |
public static String formatRuntime(long runtime)
//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(); } }