Here you can find the source of convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
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 |
public static String convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat)
//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(); } } }