Java TimeUnit Calculate printTime(long time, TimeUnit unit)

Here you can find the source of printTime(long time, TimeUnit unit)

Description

print Time

License

LGPL

Declaration

public static String printTime(long time, TimeUnit unit) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.concurrent.*;

public class Main {
    public static String printTime(long time, TimeUnit unit) {
        long ns = TimeUnit.NANOSECONDS.convert(time, unit);
        long us = TimeUnit.MICROSECONDS.convert(time, unit);
        long ms = TimeUnit.MILLISECONDS.convert(time, unit);
        long secs = TimeUnit.SECONDS.convert(time, unit);

        if (secs > 0)
            return secs + "s";
        if (ms > 0)
            return ms + "ms";
        if (us > 0)
            return us + " us";
        return ns + "ns";
    }/*from  w  w  w.j  a va 2  s  .co m*/
}

Related

  1. mapByName(TimeUnit... units)
  2. minus(Date date, int interval, TimeUnit unit)
  3. parseTimeUnit(final String unit)
  4. parseTimeUnit(Object property, TimeUnit defaultValue)
  5. pause(long duration, TimeUnit unit)
  6. randomSleep(int duration, TimeUnit timeUnit)
  7. resetWithClockStep(long clockStep, TimeUnit clockStepUnit)
  8. run(final Runnable runnable, long timeout, TimeUnit unit)
  9. shortName(TimeUnit unit)