Java Duration Format formatNanoDuration(final long nanos)

Here you can find the source of formatNanoDuration(final long nanos)

Description

Formats the given number of nanoseconds to a time like #h/m/s/ms/us/ns

License

Apache License

Parameter

Parameter Description
nanos the number of nanoseconds

Return

format the given number of nanoseconds to a time like #h/m/s/ms/us/ns

Declaration

public static String formatNanoDuration(final long nanos) 

Method Source Code

//package com.java2s;
/*//  w ww .j  a  v a2 s  .  c  om
 * Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
 * 
 * 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 {
    /**
     * Formats the given number of nanoseconds to a time like #h/m/s/ms/us/ns
     * 
     * @param nanos
     *            the number of nanoseconds
     * 
     * @return format the given number of nanoseconds to a time like #h/m/s/ms/us/ns
     */
    public static String formatNanoDuration(final long nanos) {
        if (nanos >= 3600000000000L) {
            return String.format("%.0fh", (nanos / 3600000000000.0D)); //$NON-NLS-1$
        } else if (nanos >= 60000000000L) {
            return String.format("%.0fm", (nanos / 60000000000.0D)); //$NON-NLS-1$
        } else if (nanos >= 1000000000L) {
            return String.format("%.0fs", (nanos / 1000000000.0D)); //$NON-NLS-1$
        } else if (nanos >= 1000000L) {
            return String.format("%.0fms", (nanos / 1000000.0D)); //$NON-NLS-1$
        } else if (nanos >= 1000L) {
            return String.format("%.0fus", (nanos / 1000.0D)); //$NON-NLS-1$
        } else {
            return nanos + "ns"; //$NON-NLS-1$
        }
    }
}

Related

  1. formatDurationLpad(final String s)
  2. formatDurationMills(long millis)
  3. formatDurationOneUnit(long milis)
  4. formatHuman(Duration duration)
  5. formatISO8601Duration(int[] t)
  6. formatPeriod(final Period period, final boolean wrapInDurationFunction)
  7. formatSeconds(Duration duration)
  8. formatShortDuration(long ms)
  9. formattedStringToDuration(final String formattedDuration)