From 9f6797d93172b1d79298e5ca63e995f2242f3f04 Mon Sep 17 00:00:00 2001 From: Nick Manley Date: Sat, 21 May 2016 13:53:49 -0500 Subject: [PATCH 1/2] LANG-1223: Add StopWatch#getTime(TimeUnit) --- .../apache/commons/lang3/time/StopWatch.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java index f345ba95317..7225ae20e68 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -17,6 +17,8 @@ package org.apache.commons.lang3.time; +import java.util.concurrent.TimeUnit; + /** *

* StopWatch provides a convenient API for timings. @@ -334,6 +336,26 @@ public void resume() { public long getTime() { return getNanoTime() / NANO_2_MILLIS; } + + /** + *

+ * Get the time on the stopwatch in the specified TimeUnit. + *

+ * + *

+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. The resulting time will be expressed in the desired TimeUnit with any remainder rounded down. + * For example, if the specified unit is {@code TimeUnit.HOURS} and the stopwatch time is 59 minutes, then the + * result returned will be {@code 0}. + *

+ * + * @param timeUnit the unit of time, not null + * @return the time in the specified TimeUnit, rounded down + */ + public long getTime(final TimeUnit timeUnit) { + return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS); + } + /** *

* Get the time on the stopwatch in nanoseconds. From 82a58a0b11c3086781bd526dc596e33889bd3f3a Mon Sep 17 00:00:00 2001 From: Nick Manley Date: Sat, 21 May 2016 15:06:06 -0500 Subject: [PATCH 2/2] Add String formatting to StopWatch#getTime --- .../apache/commons/lang3/time/StopWatch.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java index 7225ae20e68..faf575859ef 100644 --- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java +++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java @@ -356,6 +356,44 @@ public long getTime(final TimeUnit timeUnit) { return timeUnit.convert(getNanoTime(), TimeUnit.NANOSECONDS); } + /** + *

+ * Get the time on the stopwatch as a formatted string. + *

+ * + *

+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. The format string follows the same conventions as {@code DurationFormatUtils}. Numbers are + * left padded with zeros. + *

+ * + * @param format the way in which to format the elapsed time, not null + * @return the time as a formatted string + * @see org.apache.commons.lang3.time.DurationFormatUtils + */ + public String getTime(final String format) { + return DurationFormatUtils.formatDuration(getTime(), format); + } + + /** + *

+ * Get the time on the stopwatch as a formatted string. + *

+ * + *

+ * This is either the time between the start and the moment this method is called, or the amount of time between + * start and stop. The format string follows the same conventions as {@code DurationFormatUtils}. + *

+ * + * @param format the way in which to format the elapsed time, not null + * @param padWithZeros whether to pad the left hand side of numbers with 0's + * @return the time as a formatted string + * @see org.apache.commons.lang3.time.DurationFormatUtils + */ + public String getTime(final String format, final boolean padWithZeros) { + return DurationFormatUtils.formatDuration(getTime(), format, padWithZeros); + } + /** *

* Get the time on the stopwatch in nanoseconds.