Java Second Format formatNanoseconds (final long ns)

Here you can find the source of formatNanoseconds (final long ns)

Description

format Nanoseconds

License

Apache License

Declaration

public static String formatNanoseconds (final long ns)
    

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file

public class Main {
    public static String formatNanoseconds(final long ns) {
        String s;//  w ww.j av  a 2s  .  c o m

        final long NS_ONE_MICROSECOND = 1_000L;
        final long NS_ONE_MILLISECOND = 1_000L * NS_ONE_MICROSECOND;
        final long NS_ONE_SECOND = 1_000 * NS_ONE_MILLISECOND;
        final long NS_ONE_MINUTE = 60L * NS_ONE_SECOND;
        final long NS_ONE_HOUR = 60L * NS_ONE_MINUTE;
        final long NS_ONE_DAY = 24L * NS_ONE_HOUR;

        if (ns < NS_ONE_MICROSECOND) {
            s = String.format("%dns", ns);
        } else if (ns < NS_ONE_MILLISECOND) {
            s = String.format("%dus", ns / NS_ONE_MICROSECOND);
        } else if (ns < NS_ONE_SECOND) {
            s = String.format("%dms", ns / NS_ONE_MILLISECOND);
        } else if (ns < NS_ONE_MINUTE) {
            s = String
                    .format("%.1fs", (double) ns / (double) NS_ONE_SECOND);
        } else if (ns < NS_ONE_HOUR) {
            s = String
                    .format("%.1fm", (double) ns / (double) NS_ONE_MINUTE);
        } else if (ns < NS_ONE_DAY) {
            s = String.format("%.1fh", (double) ns / (double) NS_ONE_HOUR);
        } else {
            s = String.format("%.1fd", (double) ns / (double) NS_ONE_DAY);
        }

        return s;
    }
}

Related

  1. elapsedSeconds(long startTime)
  2. elapsedSeconds2(long milli)
  3. formatMiliSeconds(long time)
  4. formatMilliIntervalAsSeconds(long interval)
  5. formatMinutesSeconds(final long sourceDuration, final TimeUnit sourceUnit)
  6. formatSecondFractions(long numUnits, int denominator)
  7. formatSeconds(double seconds)
  8. formatSeconds(double seconds)
  9. formatSeconds(double seconds, int precision)