Java TimeUnit Usage nanoToString(final long nanos)

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

Description

nano To String

License

Apache License

Declaration

public static String nanoToString(final long nanos) 

Method Source Code


//package com.java2s;
/*/*from   w  ww  . j  a v a  2  s  .c  o  m*/
 * Copyright (C) 2008 The Guava Authors
 *
 * 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.
 */

import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MICROSECONDS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.TimeUnit;

public class Main {
    public static String nanoToString(final long nanos) {
        final TimeUnit unit = chooseUnit(nanos);
        final double value = (double) nanos / NANOSECONDS.convert(1, unit);

        // Too bad this functionality is not exposed as a regular method call
        return String.format("%.4g %s", value, abbreviate(unit));
    }

    public static TimeUnit chooseUnit(final long nanos) {
        if (DAYS.convert(nanos, NANOSECONDS) > 0) {
            return DAYS;
        }
        if (HOURS.convert(nanos, NANOSECONDS) > 0) {
            return HOURS;
        }
        if (MINUTES.convert(nanos, NANOSECONDS) > 0) {
            return MINUTES;
        }
        if (SECONDS.convert(nanos, NANOSECONDS) > 0) {
            return SECONDS;
        }
        if (MILLISECONDS.convert(nanos, NANOSECONDS) > 0) {
            return MILLISECONDS;
        }
        if (MICROSECONDS.convert(nanos, NANOSECONDS) > 0) {
            return MICROSECONDS;
        }
        return NANOSECONDS;
    }

    public static String abbreviate(final TimeUnit unit) {
        switch (unit) {
        case NANOSECONDS:
            return "ns";
        case MICROSECONDS:
            return "\u03bcs"; // ?s
        case MILLISECONDS:
            return "ms";
        case SECONDS:
            return "s";
        case MINUTES:
            return "min";
        case HOURS:
            return "h";
        case DAYS:
            return "d";
        default:
            throw new AssertionError();
        }
    }
}

Related

  1. minutesBetween(Date date1, Date date2)
  2. minutesSince(final long startNanos)
  3. nanoElapseToHumanReadable(final Long nanoElapse)
  4. nanosToHMSms(long nanos)
  5. nanosToSecs(long nanos)
  6. now()
  7. numberOfDaysSinceEpoch(long milliSinceEpoch)
  8. parseDate(String value)
  9. parseDuration(String durationStr)