Java TimeUnit Usage convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)

Here you can find the source of convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)

Description

Converts a duration in nanoseconds to a human-readable, nicely formatted string.

License

Open Source License

Parameter

Parameter Description
aTimespan the timespan in nanoseconds
aShortFormat if true, a short format for the units is chosen
aLongFormat if true, a long format for the units is chosen

Return

the formatted string

Declaration

public static String convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat,
        boolean aLongFormat) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 Rene Schneider, GEBIT Solutions GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

import java.util.concurrent.TimeUnit;

public class Main {
    /**/*from   w ww  .j a v a  2 s. c  o m*/
     * Converts a duration in nanoseconds to a human-readable, nicely formatted string.
     * 
     * @param aTimespan
     *            the timespan in nanoseconds
     * @param aShortFormat
     *            if true, a short format for the units is chosen
     * @param aLongFormat
     *            if true, a long format for the units is chosen
     * @return the formatted string
     */
    public static String convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat,
            boolean aLongFormat) {
        if (aTimespan < TimeUnit.SECONDS.toMillis(1)) {
            return aTimespan + (aShortFormat ? "ms" : aLongFormat ? " milliseconds" : "ms");
        } else {
            StringBuilder tempBuilder = new StringBuilder();

            if (aTimespan >= TimeUnit.DAYS.toNanos(1)) {
                tempBuilder.append(TimeUnit.NANOSECONDS.toDays(aTimespan)
                        + (aShortFormat ? "d" : aLongFormat ? " days" : " days"));
            }
            if (aTimespan >= TimeUnit.HOURS.toNanos(1)) {
                if (tempBuilder.length() > 0) {
                    tempBuilder.append(" ");
                }
                tempBuilder.append(TimeUnit.NANOSECONDS.toHours(aTimespan % TimeUnit.DAYS.toNanos(1))
                        + (aShortFormat ? "h" : aLongFormat ? " hours" : "hrs"));
            }
            if (aTimespan >= TimeUnit.MINUTES.toNanos(1)) {
                if (tempBuilder.length() > 0) {
                    tempBuilder.append(" ");
                }
                tempBuilder.append(TimeUnit.NANOSECONDS.toMinutes(aTimespan % TimeUnit.HOURS.toNanos(1))
                        + (aShortFormat ? "m" : aLongFormat ? " minutes" : "min"));
            }
            if (aTimespan >= TimeUnit.SECONDS.toNanos(1)) {
                if (tempBuilder.length() > 0) {
                    tempBuilder.append(" ");
                }
                tempBuilder.append(TimeUnit.NANOSECONDS.toSeconds(aTimespan % TimeUnit.MINUTES.toNanos(1))
                        + (aShortFormat ? "s" : aLongFormat ? " seconds" : "sec"));
            }
            if (aTimespan >= TimeUnit.MILLISECONDS.toNanos(1) && aTimespan < TimeUnit.MINUTES.toNanos(1)) {
                if (tempBuilder.length() > 0) {
                    tempBuilder.append(" ");
                }
                tempBuilder.append(TimeUnit.NANOSECONDS.toMillis(aTimespan % TimeUnit.SECONDS.toNanos(1))
                        + (aShortFormat ? "ms" : aLongFormat ? " milliseconds" : "msecs"));
            }

            return tempBuilder.toString();
        }
    }
}

Related

  1. calculateTime(float seconds)
  2. cancelledFuture()
  3. convertMilisecondsToDetail(long ms)
  4. convertMillis(long millis)
  5. convertMillisecondsToHHMMSS(long milliseconds)
  6. convertNanoToSeconds(long nanoTime)
  7. createTimeString(int year, int month, int day, int hours, int minutes, int seconds, String timezoneID)
  8. createTimezoneString(String timezoneID)
  9. currentMillisFromNanotime()