Java TimeUnit Usage formatTimeDuration(long duration)

Here you can find the source of formatTimeDuration(long duration)

Description

This method is used to format a timestamp to 'dd:hh:mm:ss'.

License

Open Source License

Parameter

Parameter Description
duration timestamp duration.

Return

formatted time duration to 'dd:hh:mm:ss'.

Declaration

public static String formatTimeDuration(long duration) 

Method Source Code

//package com.java2s;
/*/*from   w  w w .j  av a2s  . c  om*/
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Duration seconds format.
     * This will produce a two digit number for seconds followed with character 's'.
     * Example 08s.
     */
    private static final String durationSecondsFormat = "%02ds";
    /**
     * Duration minutes and seconds format.
     * This will provide a two digit number for minutes followed by character 'm' and a two digit number for seconds
     * followed with character 's'.
     * Example: 01m:23s.
     */
    private static final String durationMinutesSecondsFormat = "%02dm:%02ds";
    /**
     * Duration hours, minutes and seconds format. This will provide a two digit number for hours followed by
     * character 'h', a two digit number for minutes followed with character 'm' and a two digit number for seconds
     * followed with character 's'.
     * Example 07h:12m:09s.
     */
    private static final String durationHoursMinutesSecondsFormat = "%02dh:%02dm:%02ds";
    /**
     * Duration days, hours, minutes and seconds format.
     * This will produce a number for days followed with character 'd', a two digit number for hours followed by
     * character 'h', a two digit number for minutes followed with character 'm' and a two digit number for seconds
     * followed with character 's'.
     */
    private static final String durationDaysHoursMinutesSecondsFormat = "%dd:%02dh:%02dm:%02ds";

    /**
     * This method is used to format a timestamp to 'dd:hh:mm:ss'.
     *
     * @param duration  timestamp duration.
     * @return          formatted time duration to 'dd:hh:mm:ss'.
     */
    public static String formatTimeDuration(long duration) {
        String timeDuration;
        long days = TimeUnit.MILLISECONDS.toDays(duration);
        long hours = TimeUnit.MILLISECONDS.toHours(duration)
                - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
        // Setting the duration to a readable format.
        if (days == 0 && hours == 0 && minutes == 0) {
            timeDuration = String.format(durationSecondsFormat, seconds);
        } else if (days == 0 && hours == 0) {
            timeDuration = String.format(durationMinutesSecondsFormat, minutes, seconds);
        } else if (days == 0) {
            timeDuration = String.format(durationHoursMinutesSecondsFormat, hours, minutes, seconds);
        } else {
            timeDuration = String.format(durationDaysHoursMinutesSecondsFormat, days, hours, minutes, seconds);
        }
        return timeDuration;
    }
}

Related

  1. formatSeconds(int seconds)
  2. formatTime(long time)
  3. formatTime(long time)
  4. formatTime(long timeInNanoseconds)
  5. formatTime(String format, double nanoseconds)
  6. formatTimeInMilliSeconds(long duration)
  7. formatUptime(long startTime)
  8. formatUptime(long uptimeInMs)
  9. generateRandomDateTimeForLogEvent(Date laterThan)