public final class Duration extends Object
Represents a period of time (for example, "2 hours and 30 minutes").
This class is immutable. Use the builder()
method to construct a new
instance, or the parse(java.lang.String)
method to parse a duration string.
Examples:
Duration duration = Duration.builder().hours(2).minutes(30).build(); Duration duration = Duration.parse("PT2H30M"); //add a duration value to a Date Date start = ... Date end = duration.add(start);
Modifier and Type | Class and Description |
---|---|
static class |
Duration.Builder
Builds
Duration objects. |
Modifier and Type | Method and Description |
---|---|
Date |
add(Date date)
Adds this duration value to a
Date object. |
static Duration.Builder |
builder()
Creates a builder object for constructing new instances of this class.
|
static Duration |
diff(Date start,
Date end)
Builds a duration based on the difference between two dates.
|
boolean |
equals(Object obj) |
static Duration |
fromMillis(long milliseconds)
Builds a duration from a number of milliseconds.
|
Integer |
getDays()
Gets the number of days.
|
Integer |
getHours()
Gets the number of hours.
|
Integer |
getMinutes()
Gets the number of minutes.
|
Integer |
getSeconds()
Gets the number of seconds.
|
Integer |
getWeeks()
Gets the number of weeks.
|
int |
hashCode() |
boolean |
hasTime()
Determines if any time components are present.
|
boolean |
isPrior()
Gets whether the duration is negative.
|
static Duration |
parse(String value)
Parses a duration string.
|
long |
toMillis()
Converts the duration value to milliseconds.
|
String |
toString()
Converts the duration to its string representation.
|
public static Duration parse(String value)
value
- the duration string (e.g. "P30DT10H")IllegalArgumentException
- if the duration string is invalidpublic static Duration diff(Date start, Date end)
start
- the start dateend
- the end datepublic static Duration fromMillis(long milliseconds)
milliseconds
- the number of millisecondspublic static Duration.Builder builder()
public boolean isPrior()
public Integer getWeeks()
public Integer getDays()
public Integer getHours()
public Integer getMinutes()
public Integer getSeconds()
public Date add(Date date)
Date
object.date
- the date to add topublic long toMillis()
isPrior()
is true)public boolean hasTime()
Copyright © 2013-2015 Michael Angstadt. All Rights Reserved.