public class Duration extends java.lang.Object implements java.io.Serializable, java.lang.Comparable<Duration>
PnDTnHnMnS
, with 'P' and 'T' optional. Days are considered to be exactly 24
hours.
Similarly to the java.time.Duration
class, this class does not support year or month sections in the format.
This implementation does not support fractions or negative values.
parse(CharSequence)
,
Serialized FormModifier and Type | Field and Description |
---|---|
private static int |
HOURS_PER_DAY
Hours per day.
|
private static int |
MINUTES_PER_HOUR
Minutes per hour.
|
private static java.util.regex.Pattern |
PATTERN
The pattern for parsing.
|
private long |
seconds
The number of seconds in the duration.
|
private static int |
SECONDS_PER_DAY
Seconds per day.
|
private static int |
SECONDS_PER_HOUR
Seconds per hour.
|
private static int |
SECONDS_PER_MINUTE
Seconds per minute.
|
private static long |
serialVersionUID |
static Duration |
ZERO
Constant for a duration of zero.
|
Modifier | Constructor and Description |
---|---|
private |
Duration(long seconds)
Constructs an instance of
Duration using seconds. |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(Duration other) |
private static Duration |
create(long seconds)
Obtains an instance of
Duration using seconds. |
private static Duration |
create(long daysAsSecs,
long hoursAsSecs,
long minsAsSecs,
long secs) |
boolean |
equals(java.lang.Object obj) |
int |
hashCode() |
static Duration |
parse(java.lang.CharSequence text)
Obtains a
Duration from a text string such as PnDTnHnMnS . |
private static long |
parseNumber(java.lang.CharSequence text,
java.lang.String parsed,
int multiplier,
java.lang.String errorText) |
long |
toMillis()
Converts this duration to the total length in milliseconds.
|
java.lang.String |
toString()
A string representation of this duration using ISO-8601 seconds based representation, such as
PT8H6M12S . |
private static final long serialVersionUID
public static final Duration ZERO
private static final int HOURS_PER_DAY
private static final int MINUTES_PER_HOUR
private static final int SECONDS_PER_MINUTE
private static final int SECONDS_PER_HOUR
private static final int SECONDS_PER_DAY
private static final java.util.regex.Pattern PATTERN
private final long seconds
private Duration(long seconds)
Duration
using seconds.seconds
- the length of the duration in seconds, positive or negativepublic static Duration parse(java.lang.CharSequence text)
Duration
from a text string such as PnDTnHnMnS
.
This will parse a textual representation of a duration, including the string produced by toString()
. The
formats accepted are based on the ISO-8601 duration format PnDTnHnMnS
with days considered to be exactly
24 hours.
This implementation does not support negative numbers or fractions (so the smallest non-zero value a Duration can have is one second).
The string optionally starts with the ASCII letter "P" in upper or lower case. There are then four sections, each
consisting of a number and a suffix. The sections have suffixes in ASCII of "D", "H", "M" and "S" for days,
hours, minutes and seconds, accepted in upper or lower case. The suffixes must occur in order. The ASCII letter
"T" may occur before the first occurrence, if any, of an hour, minute or second section. At least one of the four
sections must be present, and if "T" is present there must be at least one section after the "T". The number part
of each section must consist of one or more ASCII digits. The number may not be prefixed by the ASCII negative or
positive symbol. The number of days, hours, minutes and seconds must parse to a long
.
Examples:
"PT20S" -- parses as "20 seconds" "PT15M" -- parses as "15 minutes" (where a minute is 60 seconds) "PT10H" -- parses as "10 hours" (where an hour is 3600 seconds) "P2D" -- parses as "2 days" (where a day is 24 hours or 86400 seconds) "P2DT3H4M" -- parses as "2 days, 3 hours and 4 minutes"
text
- the text to parse, not nulljava.lang.IllegalArgumentException
- if the text cannot be parsed to a durationprivate static long parseNumber(java.lang.CharSequence text, java.lang.String parsed, int multiplier, java.lang.String errorText)
private static Duration create(long daysAsSecs, long hoursAsSecs, long minsAsSecs, long secs)
private static Duration create(long seconds)
Duration
using seconds.seconds
- the length of the duration in seconds, positive onlypublic long toMillis()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
PT8H6M12S
.
The format of the returned string will be PnDTnHnMnS
, where n is the relevant days, hours, minutes or
seconds part of the duration. If a section has a zero value, it is omitted. The hours, minutes and seconds are
all positive.
Examples:
"20 seconds" -- "PT20S "15 minutes" (15 * 60 seconds) -- "PT15M" "10 hours" (10 * 3600 seconds) -- "PT10H" "2 days" (2 * 86400 seconds) -- "P2D"
toString
in class java.lang.Object