public final class Duration extends Object implements Comparable<Duration>, Serializable
long value, thus some methods
on this class are subject to overflow.| Modifier and Type | Field and Description |
|---|---|
static Duration |
ONE_DAY
One day.
|
static Duration |
ONE_HOUR
One hour.
|
static Duration |
ONE_MILLISECOND
One millisecond.
|
static Duration |
ONE_MINUTE
One minute.
|
static Duration |
ONE_SECOND
One second.
|
static Duration |
ONE_WEEK
One week.
|
static Duration |
ZERO
Zero millisecond.
|
| Modifier and Type | Method and Description |
|---|---|
Duration |
absoluteValue()
Returns a copy of this duration with a positive length.
|
Date |
after(Date d)
Returns the date obtained by adding this duration to the given date.
|
Date |
before(Date d)
Returns the date obtained by subtracting this duration from the given
date.
|
static Duration |
between(Date start,
Date end)
Creates a new
Duration instance representing the amount of
time elapsed between the given dates. |
int |
compareTo(Duration d) |
Duration |
dividedBy(int divisor)
Returns the result of dividing this duration by the given divisor.
|
boolean |
equals(Object o) |
int |
hashCode() |
Duration |
minus(Duration d)
Returns the result of subtracting the given duration from this one.
|
Duration |
multipliedBy(int factor)
Returns the result of multiplying this duration by the given factor.
|
Duration |
negated()
Returns the result of negating this duration.
|
static Duration |
of(Duration... durations)
Creates a new
Duration instance by summing all the given
Durations' values. |
static Duration |
of(long amount,
TimeUnit unit)
Creates a new
Duration representing the given amount in the
given unit. |
static Duration |
parse(String duration)
Parses the given duration's
String representation into a
Duration instance. |
Duration |
plus(Duration d)
Returns the result of adding the given duration to this duration.
|
int |
sign()
Returns the sign of this duration, namely it returns
1 if
this duration is positive, 0 if it is equal to 0, and
-1 if it is negative. |
static Duration |
since(Date d)
Creates a new
Duration instance representing the amount of
time elapsed since the given date. |
long |
to(TimeUnit unit)
Converts this duration into the given unit.
|
long |
toMilliseconds()
Returns the number of milliseconds in this duration.
|
String |
toString() |
static Duration |
until(Date d)
Creates a new
Duration instance representing the amount of
time that will elapse until the given date. |
public static final Duration ZERO
public static final Duration ONE_MILLISECOND
public static final Duration ONE_SECOND
public static final Duration ONE_MINUTE
public static final Duration ONE_HOUR
public static final Duration ONE_DAY
public static final Duration ONE_WEEK
public static Duration parse(String duration)
String representation into a
Duration instance. The given String must be a list
of value/unit pairs separated by ' ', '+', '-', '\n', '\t', '\r',
'&' or ',' characters or by the "and" String. Values
must be integer (either positive or negative) values. Valid units are:
"milliseconds", "millisecond", "millis" and "ms" for milliseconds,
"seconds", "second", "sec", "secs" and "s" for seconds, "minutes",
"minute", "min", "mins" and "m" for minutes, "hours", "hour" and "h"
for hours, "days", "day" and "d" for days. Values must be separated
from their unit by a whitespace character. Parsing is not case
sensitive.duration - the String to parse into a Duration.Duration.NullPointerException - if duration is null.IllegalArgumentException - if duration can't be parsed.ArithmeticException - if the total duration value overflows the
storage capacity of this class.public static Duration of(long amount, TimeUnit unit)
Duration representing the given amount in the
given unit.amount - the amount.unit - the unit in which the amout is given.Duration instance.NullPointerException - if unit is null.ArithmeticException - if the resulting duration is too large.public static Duration of(Duration... durations)
Duration instance by summing all the given
Durations' values.durations - the Durations to sum.Duration.NullPointerException - if durations is null or
if it contains null reference.ArithmeticException - if the resulting duration is too large.public static Duration between(Date start, Date end)
Duration instance representing the amount of
time elapsed between the given dates.start - the start date.end - the end date.start and end.NullPointerException - if one of the arguments is null.public static Duration since(Date d)
Duration instance representing the amount of
time elapsed since the given date.d - the start date.d.NullPointerException - if d is null.public static Duration until(Date d)
Duration instance representing the amount of
time that will elapse until the given date.d - the end date.d.NullPointerException - if d is null.public Duration plus(Duration d)
d - the duration to add.NullPointerException - if d is null.ArithmeticException - if the resulting duration is too large.public Duration minus(Duration d)
d - the duration to subtract.NullPointerException - if d is null.ArithmeticException - if the resulting duration is too large.public Duration multipliedBy(int factor)
factor - the multiplication factor.ArithmeticException - if the resulting duration is too large.public Duration dividedBy(int divisor)
divisor - the divisor.ArithmeticException - if the resulting duration is too large.public int sign()
1 if
this duration is positive, 0 if it is equal to 0, and
-1 if it is negative.public Duration negated()
ArithmeticException - if the resulting duration is too large.public Duration absoluteValue()
ArithmeticException - if the resulting duration is too large.public Date after(Date d)
d - the origin date.ArithmeticException - if the resulting duration is too large.public Date before(Date d)
d - the origin date.ArithmeticException - if the resulting duration is too large.public long to(TimeUnit unit)
unit - the target unit.NullPointerException - if unit is null.public long toMilliseconds()
public int compareTo(Duration d)
compareTo in interface Comparable<Duration>Copyright © 2012–2015. All rights reserved.