/** * Returns the current elapsed time shown on this stopwatch, expressed in the desired time unit, * with any fraction rounded down. * * <p><b>Note:</b> the overhead of measurement can be more than a microsecond, so it is generally * not useful to specify {@link TimeUnit#NANOSECONDS} precision here. * * <p>It is generally not a good idea to use an ambiguous, unitless {@code long} to represent * elapsed time. Therefore, we recommend using {@link #elapsed()} instead, which returns a * strongly-typed {@link Duration} instance. * * @since 14.0 (since 10.0 as {@code elapsedTime()}) */ public long elapsed(TimeUnit desiredUnit) { return desiredUnit.convert(elapsedNanos(), NANOSECONDS); }
/** Returns a string representation of the current elapsed time. */ @Override public String toString() { long nanos = elapsedNanos(); TimeUnit unit = chooseUnit(nanos); double value = (double) nanos / NANOSECONDS.convert(1, unit); // Too bad this functionality is not exposed as a regular method call return Platform.formatCompact4Digits(value) + " " + abbreviate(unit); }
/** * Returns the current elapsed time shown on this stopwatch as a {@link Duration}. Unlike {@link * #elapsed(TimeUnit)}, this method does not lose any precision due to rounding. * * @since 22.0 */ @GwtIncompatible @J2ObjCIncompatible public Duration elapsed() { return Duration.ofNanos(elapsedNanos()); }