Package org.nuxeo.common.utils
Class PeriodAndDuration
- java.lang.Object
-
- org.nuxeo.common.utils.PeriodAndDuration
-
- All Implemented Interfaces:
TemporalAmount
public final class PeriodAndDuration extends Object implements TemporalAmount
The combination of aPeriod
and aDuration
.This allows the representation of ISO 8601 "durations", which comprise a nominal duration (Java
Period
, i.e., years, months, days), and an accurate duration (JavaDuration
, i.e., hours, minutes, seconds).- Since:
- 11.1
-
-
Field Summary
Fields Modifier and Type Field Description Duration
duration
protected static Pattern
PATTERN
Period
period
protected static int
SECONDS_PER_HOUR
protected static int
SECONDS_PER_MINUTE
protected static List<TemporalUnit>
UNITS
The set of supported units.static PeriodAndDuration
ZERO
A constant for a period and duration of zero.
-
Constructor Summary
Constructors Constructor Description PeriodAndDuration(Duration duration)
Constructs aPeriodAndDuration
from the given duration.PeriodAndDuration(Period period)
Constructs aPeriodAndDuration
from the given period.PeriodAndDuration(Period period, Duration duration)
Constructs aPeriodAndDuration
from the given period and duration.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Temporal
addTo(Temporal temporal)
long
get(TemporalUnit unit)
List<TemporalUnit>
getUnits()
static PeriodAndDuration
parse(String text)
Obtains aPeriodAndDuration
from a text string.protected static int
parseFraction(String string, int sign, String text)
protected static int
parseInt(String string, String text, String errorText)
protected static long
parseNumber(String string, int multiplier, String text, String errorText)
Temporal
subtractFrom(Temporal temporal)
String
toString()
Outputs this period and duration as aString
, such asP6Y3M1DT4H12M5.636224S
.
-
-
-
Field Detail
-
ZERO
public static final PeriodAndDuration ZERO
A constant for a period and duration of zero.
-
UNITS
protected static final List<TemporalUnit> UNITS
-
PATTERN
protected static final Pattern PATTERN
-
SECONDS_PER_MINUTE
protected static final int SECONDS_PER_MINUTE
- See Also:
- Constant Field Values
-
SECONDS_PER_HOUR
protected static final int SECONDS_PER_HOUR
- See Also:
- Constant Field Values
-
period
public final Period period
-
duration
public final Duration duration
-
-
Constructor Detail
-
PeriodAndDuration
public PeriodAndDuration(Period period, Duration duration)
Constructs aPeriodAndDuration
from the given period and duration.
-
PeriodAndDuration
public PeriodAndDuration(Period period)
Constructs aPeriodAndDuration
from the given period.
-
PeriodAndDuration
public PeriodAndDuration(Duration duration)
Constructs aPeriodAndDuration
from the given duration.
-
-
Method Detail
-
getUnits
public List<TemporalUnit> getUnits()
- Specified by:
getUnits
in interfaceTemporalAmount
-
get
public long get(TemporalUnit unit)
- Specified by:
get
in interfaceTemporalAmount
-
addTo
public Temporal addTo(Temporal temporal)
- Specified by:
addTo
in interfaceTemporalAmount
-
subtractFrom
public Temporal subtractFrom(Temporal temporal)
- Specified by:
subtractFrom
in interfaceTemporalAmount
-
parse
public static PeriodAndDuration parse(String text)
Obtains aPeriodAndDuration
from a text string.This will parse the string based on the ISO-8601 period format
PnYnMnDTnHnMnS
. A leading minus sign, and negative values for the units, are allowed.- Parameters:
text
- the text to parse, notnull
- Returns:
- the period and duration (never
null
) - Throws:
DateTimeParseException
- if the text cannot be parsed to a period and duration- See Also:
toString()
-
parseNumber
protected static long parseNumber(String string, int multiplier, String text, String errorText)
-
toString
public String toString()
Outputs this period and duration as aString
, such asP6Y3M1DT4H12M5.636224S
.The output will be in the ISO-8601 period format. A zero period will be represented as zero seconds, "PT0S".
- Overrides:
toString
in classObject
- Returns:
- a string representation of this period, not
null
- See Also:
parse(java.lang.String)
-
-