public class Duration extends LongValue
Duration
is an immutable length of time stored as a number of milliseconds.
Various factory and conversion methods are available for convenience.
These static factory methods allow easy construction of value objects using either long values
like seconds(2034)
or hours(3)
:
Duration.milliseconds(long)
Duration.seconds(int)
Duration.minutes(int)
Duration.hours(int)
Duration.days(int)
...or double-precision floating point values like days(3.2)
:
Duration.milliseconds(double)
Duration.seconds(double)
Duration.minutes(double)
Duration.hours(double)
Duration.days(double)
In the case of milliseconds(double)
, the value will be rounded off to the nearest
integral millisecond using Math.round()
.
The precise number of milliseconds represented by a Duration
object can be retrieved
by calling the getMilliseconds
method. The value of a Duration
object
in a given unit like days or hours can be retrieved by calling one of the following unit methods,
each of which returns a double-precision floating point number:
seconds()
minutes()
hours()
days()
Values can be added and subtracted using the add(Duration)
and
subtract(Duration)
methods, each of which returns a new immutable
Duration
object.
String
values can be converted to Duration
objects using the static
valueOf
factory methods. The String
format is the opposite of the one
created by toString()
, which converts a Duration
object to a readable
form, such as "3.2 hours" or "32.5 minutes". Valid units are: milliseconds, seconds, minutes
hours and days. Correct English plural forms are used in creating String
values and
are parsed as well. The Locale
is respected and "," will be used instead of "." in
the Eurozone.
The benchmark method will "benchmark" a Runnable
or an ICode
implementing
object, returning a Duration
object that represents the amount of time elapsed in
running the code.
Finally, the sleep
method will sleep for the value of a Duration
.
Modifier and Type | Field and Description |
---|---|
static Duration |
MAXIMUM
Constant for maximum duration.
|
static Duration |
NONE
Constant for no duration.
|
static Duration |
ONE_DAY
Constant for one day.
|
static Duration |
ONE_HOUR
Constant for one hour.
|
static Duration |
ONE_MINUTE
Constant for on minute.
|
static Duration |
ONE_SECOND
Constant for one second.
|
static Duration |
ONE_WEEK
Constant for one week.
|
Modifier | Constructor and Description |
---|---|
protected |
Duration(long milliseconds)
Private constructor forces use of static factory methods.
|
Modifier and Type | Method and Description |
---|---|
Duration |
add(Duration duration)
Adds a given
Duration to this Duration . |
static Duration |
benchmark(ICode code,
org.slf4j.Logger log)
Benchmark the given command.
|
static Duration |
benchmark(Runnable code)
Benchmark the given command.
|
double |
days()
Retrieves the number of days of the current
Duration . |
static Duration |
days(double days)
Retrieves the
Duration based on days. |
static Duration |
days(int days)
Retrieves the
Duration based on days. |
static Duration |
elapsed(Time start)
Calculates the amount of time elapsed since start time.
|
long |
getMilliseconds()
Retrieves the number of milliseconds in this
Time value. |
double |
hours()
Retrieves the number of hours of the current
Duration . |
static Duration |
hours(double hours)
Retrieves the
Duration based on hours. |
static Duration |
hours(int hours)
Retrieves the
Duration based on hours. |
static Duration |
milliseconds(double milliseconds)
Retrieves the
Duration based on milliseconds. |
static Duration |
milliseconds(long milliseconds)
Retrieves the
Duration based on milliseconds. |
double |
minutes()
Retrieves the number of minutes of the current
Duration . |
static Duration |
minutes(double minutes)
Retrieves the
Duration based on minutes. |
static Duration |
minutes(int minutes)
Retrieves the
Duration based on minutes. |
double |
seconds()
Retrieves the number of seconds of the current
Duration . |
static Duration |
seconds(double seconds)
Retrieves the
Duration based on seconds. |
static Duration |
seconds(int seconds)
Retrieves the
Duration based on seconds. |
void |
sleep()
Sleeps for the current
Duration . |
Duration |
subtract(Duration that)
Subtracts a given
Duration from this Duration . |
String |
toString()
Retrieves the
String representation of this Duration in days,
hours, minutes, seconds or milliseconds, as appropriate. |
String |
toString(Locale locale)
Retrieves the
String representation of this Duration in days,
hours, minutes, seconds or milliseconds, as appropriate. |
static Duration |
valueOf(long time)
Retrieves the given
long as a Duration . |
static Duration |
valueOf(String string)
Converts the given
String to a new Duration object. |
static Duration |
valueOf(String string,
Locale locale)
Converts the given
String to a new Duration object. |
void |
wait(Object object)
Wait for this duration on the given monitor
|
compareTo, equals, greaterThan, greaterThan, greaterThanOrEqual, greaterThanOrEqual, hashCode, lessThan, lessThan, lessThanOrEqual, lessThanOrEqual, max, maxNullSafe, min
public static final Duration MAXIMUM
public static final Duration NONE
public static final Duration ONE_DAY
public static final Duration ONE_HOUR
public static final Duration ONE_MINUTE
public static final Duration ONE_SECOND
public static final Duration ONE_WEEK
protected Duration(long milliseconds)
milliseconds
- number of milliseconds in this Duration
public static Duration benchmark(ICode code, org.slf4j.Logger log)
code
- an ICode
log
- optional logger to use with errors and exceptionsTime
value it took to run the codepublic static Duration benchmark(Runnable code)
code
- a Runnable
Time
value it took to run the codepublic static Duration days(double days)
Duration
based on days.days
- days double
valueDuration
based on dayspublic static Duration days(int days)
Duration
based on days.days
- days int
valueDuration
based on dayspublic static Duration elapsed(Time start)
start
- the start Time
Duration
IllegalStateException
- if start Time
is in the futurepublic static Duration hours(double hours)
Duration
based on hours.hours
- hours double
valueDuration
based on hourspublic static Duration hours(int hours)
Duration
based on hours.hours
- hours int
valueDuration
based on hourspublic static Duration milliseconds(double milliseconds)
Duration
based on milliseconds.milliseconds
- milliseconds double
valueDuration
based on millisecondspublic static Duration milliseconds(long milliseconds)
Duration
based on milliseconds.milliseconds
- milliseconds long
valueDuration
based on millisecondspublic static Duration minutes(double minutes)
Duration
based on minutes.minutes
- minutes double
valueDuration
based on minutespublic static Duration minutes(int minutes)
Duration
based on minutes.minutes
- minutes int
valueDuration
based on minutespublic static Duration seconds(double seconds)
Duration
based on seconds.seconds
- seconds double
valueDuration
based on secondspublic static Duration seconds(int seconds)
Duration
based on seconds.seconds
- seconds int
valueDuration
based on secondspublic static Duration valueOf(long time)
long
as a Duration
.time
- the duration long
value in millisecondsDuration
valuepublic static Duration valueOf(String string) throws StringValueConversionException
String
to a new Duration
object. The string can
take the form of a floating point number followed by a number of milliseconds, seconds,
minutes, hours or days. For example "6 hours" or "3.4 days". Parsing is case-insensitive.string
- a String
to parseDuration
value of the given String
StringValueConversionException
public static Duration valueOf(String string, Locale locale) throws StringValueConversionException
String
to a new Duration
object. The string can
take the form of a floating point number followed by a number of milliseconds, seconds,
minutes, hours or days. For example "6 hours" or "3.4 days". Parsing is case-insensitive.string
- a String
to parselocale
- the Locale
used for parsingDuration
value of the given String
StringValueConversionException
public Duration add(Duration duration)
Duration
to this Duration
.duration
- the Duration
to addDuration
spublic final double days()
Duration
.Duration
public final double hours()
Duration
.Duration
public final double minutes()
Duration
.Duration
public final double seconds()
Duration
.Duration
public final void sleep()
Duration
.public Duration subtract(Duration that)
Duration
from this Duration
.that
- the Duration
to subtractDuration
minus that Duration
public void wait(Object object)
object
- The monitor to wait onpublic String toString()
String
representation of this Duration
in days,
hours, minutes, seconds or milliseconds, as appropriate. Uses the default Locale
.public String toString(Locale locale)
String
representation of this Duration
in days,
hours, minutes, seconds or milliseconds, as appropriate.locale
- a Locale
String
representationpublic final long getMilliseconds()
Time
value.Time
valueCopyright © 2006–2022 Apache Software Foundation. All rights reserved.