Java Duration Format formatDuration(long duration, boolean displayMilliseconds)

Here you can find the source of formatDuration(long duration, boolean displayMilliseconds)

Description

Formats a duration in milliseconds.

License

Apache License

Parameter

Parameter Description
duration The duration in milliseconds to format.
displayMilliseconds Whether to display milliseconds or not.

Return

The formatted duration.

Declaration

public static String formatDuration(long duration, boolean displayMilliseconds) 

Method Source Code

//package com.java2s;
/*//from  w w w . j  av a  2s. c  o m
* Copyright 2015 herd contributors
*
* 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 {
    public static final long MILLIS_IN_ONE_SECOND = 1000;
    public static final long MILLIS_IN_ONE_MINUTE = MILLIS_IN_ONE_SECOND * 60;
    public static final long MILLIS_IN_ONE_HOUR = MILLIS_IN_ONE_MINUTE * 60;
    public static final long MILLIS_IN_ONE_DAY = MILLIS_IN_ONE_HOUR * 24;

    /**
     * Formats a duration in milliseconds.
     *
     * @param duration The duration in milliseconds to format.
     * @param displayMilliseconds Whether to display milliseconds or not.
     *
     * @return The formatted duration.
     */
    public static String formatDuration(long duration, boolean displayMilliseconds) {
        long remainingDuration = duration;

        // If the duration is 0, then just return 0 with the correct granularity.
        if (remainingDuration == 0) {
            return displayMilliseconds ? "0 Milliseconds" : "0 Seconds";
        }

        // Initialize the result string.
        StringBuilder result = new StringBuilder();

        // Compute each duration separately.
        remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_DAY, "Day", result, false);
        remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_HOUR, "Hour", result, false);
        remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_MINUTE, "Minute", result, false);
        remainingDuration = processDuration(remainingDuration, MILLIS_IN_ONE_SECOND, "Second", result, false);

        // Compute the milliseconds.
        long milliSeconds = remainingDuration;
        if (milliSeconds > 0) {
            if (displayMilliseconds) {
                // Just display the final millisecond portion no matter what (i.e. the duration is 1).
                processDuration(remainingDuration, 1, "Millisecond", result, true);
            } else {
                // If no milliseconds are being displayed and the time is less than 1 second, return 0 seconds.
                if (result.length() == 0) {
                    result.append("0 Seconds");
                }
            }
        }

        // Return the result.
        return result.toString();
    }

    /**
     * Process a single duration.
     *
     * @param remainingDuration the remaining duration in milliseconds.
     * @param millisPerDuration the number of milliseconds per one duration (e.g. 1000 milliseconds in a single second).
     * @param durationName the duration name (e.g. "Day").
     * @param result the result string.
     * @param displayZeroDuration Flag that indicates whether a "0" duration should be displayed or not.
     *
     * @return the new remaining duration in milliseconds after the current duration is substracted from the original remaining duration.
     */
    private static long processDuration(long remainingDuration, long millisPerDuration, String durationName,
            StringBuilder result, boolean displayZeroDuration) {
        // Compute how many durations (e.g. "5" days).
        long duration = remainingDuration / millisPerDuration;

        // Compute the new remaining duration which is the previous remaining duration - the new duration in milliseconds.
        long newRemainingDuration = remainingDuration - (duration * millisPerDuration);

        // Only append the duration to the result if some time exists in the duration (e.g. we don't add "0 Days").
        if (duration > 0 || displayZeroDuration) {
            // If the result previously had a value, add a comma to separate this duration from the previous durations (e.g. 5 days"," ...).
            if (result.length() > 0) {
                result.append(", ");
            }

            // Append the duration along with the duration name (e.g. "5 day").
            result.append(String.valueOf(duration)).append(' ').append(durationName);

            // If the duration is > 1, then make it plural (e.g. 5 day"s").
            if (duration > 1) {
                result.append('s');
            }
        }

        // Return the new remaining duration so the calculation can continue.
        return newRemainingDuration;
    }
}

Related

  1. formatDuration(int seconds)
  2. formatDuration(long duration)
  3. formatDuration(long duration)
  4. formatDuration(long duration)
  5. formatDuration(long duration)
  6. formatDuration(long duration, boolean simplify, boolean includeMillies)
  7. formatDuration(long durationInMs)
  8. formatDuration(long durationMillis)
  9. formatDuration(long durationSec)