Here you can find the source of formatBuildDuration(final long duration)
Parameter | Description |
---|---|
duration | The duration to set |
public static String formatBuildDuration(final long duration)
//package com.java2s; /*// w w w. j a v a 2 s . c o m * JenkinsRemote - A tool that sits in the tray and provide Jenkins information/control. * * Copyright 2009 SP extreme (http://www.spextreme.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. */ public class Main { /** * Represents the number of milliseconds in a second. */ public static final int MILLISECONDS = 1000; /** * Represents the number of minutes in a hour. */ public static final int MINUTES = 60; /** * Represents the number of seconds in a minute. */ public static final int SECONDS = 60; /** * Takes a duration and formats it as h:m:s:ms. If hours minutes or seconds are 0 then they will * not be displayed if the previous item is not set. * * @param duration The duration to set * @return The formatted output. */ public static String formatBuildDuration(final long duration) { final StringBuilder formattedDuration = new StringBuilder(); final int hours = (int) Math.floor(duration / (MILLISECONDS * MINUTES * SECONDS)); final int minutes = (int) Math .floor(duration % (MILLISECONDS * MINUTES * SECONDS) / (MILLISECONDS * MINUTES)); final int seconds = (int) Math .floor(duration % (MILLISECONDS * MINUTES * SECONDS) % (MILLISECONDS * MINUTES) / MILLISECONDS); // Format the hours if needed boolean hoursAdded = false; if (hours != 0) { formattedDuration.append(hours); formattedDuration.append(hours == 1 ? " hr " : " hrs "); hoursAdded = true; } // Format minutes if needed boolean minutesAdded = false; if ((minutes != 0) || hoursAdded) { formattedDuration.append(minutes); formattedDuration.append(minutes == 1 ? " min " : " mins "); minutesAdded = true; } // Format seconds if needed if ((seconds != 0) || minutesAdded) { formattedDuration.append(seconds); formattedDuration.append(seconds == 1 ? " sec " : " secs "); } return formattedDuration.toString(); } }