Java Duration Format formatBuildDuration(final long duration)

Here you can find the source of formatBuildDuration(final long duration)

Description

Takes a duration and formats it as h:m:s:ms.

License

Apache License

Parameter

Parameter Description
duration The duration to set

Return

The formatted output.

Declaration

public static String formatBuildDuration(final long duration) 

Method Source Code

//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();
    }
}

Related

  1. durationToString(long durationMs)
  2. durationToString(Long ms)
  3. durationToString(long x)
  4. durationTranslator(String sonarDuration)
  5. format8601Duration(long duration)
  6. formatDuration(double dblSeconds)
  7. formatDuration(Duration d)
  8. formatDuration(Duration duration)
  9. formatDuration(Duration duration)