T - type of temporal amount (must be Duration or Period or subclass of TemporalAmount)public abstract class TemporalAmountParser<T extends java.time.temporal.TemporalAmount>
extends java.lang.Object
Period and Duration.
It can parse both estimated and exact temporal amount. Estimated temporal amount is a temporal amount that is not exact. For example, if you want to parse 1 month, you can write 1mo or 30d. If you write 1mo, it can be estimated to 28, 29, 30 or 31 days. If you write 1y, it can be estimated to 365 or 366 days.
This class is immutable.
You can create new instance using constructor PeriodParser() or DurationParser().
You can also use DurationParser.DATE_TIME_UNITS, PeriodParser.DATE_UNITS and DurationParser.TIME_UNITS.
You can add new units using withUnit(String, ChronoUnit).
Use parse(String) to parse temporal amount from string.
Use format(TemporalAmount) to format temporal amount to string.
| Modifier and Type | Class and Description |
|---|---|
static interface |
TemporalAmountParser.LocalDateTimeProvider
A provider for
LocalDateTime. |
protected static class |
TemporalAmountParser.TemporalEntry |
protected static interface |
TemporalAmountParser.TimeModifier |
| Modifier | Constructor and Description |
|---|---|
protected |
TemporalAmountParser(java.time.temporal.ChronoUnit defaultZero,
java.util.Map<java.lang.String,java.time.temporal.ChronoUnit> units,
java.util.Set<TemporalAmountParser.TimeModifier> modifiers,
TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) |
protected |
TemporalAmountParser(java.time.temporal.ChronoUnit defaultZero,
TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) |
| Modifier and Type | Method and Description |
|---|---|
protected abstract TemporalAmountParser<T> |
clone(java.time.temporal.ChronoUnit defaultZeroUnit,
java.util.Map<java.lang.String,java.time.temporal.ChronoUnit> units,
java.util.Set<TemporalAmountParser.TimeModifier> modifiers,
TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) |
java.lang.String |
format(T temporalAmount)
Formats the given estimated temporal amount to a string.
|
protected abstract T |
getZero() |
protected abstract T |
negate(T temporalAmount) |
T |
parse(java.lang.String input)
Parses the given string and returns the estimated temporal amount.]
|
protected abstract T |
plus(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation,
T temporalAmount,
TemporalAmountParser.TemporalEntry temporalEntry) |
protected abstract java.time.Duration |
toDuration(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation,
T temporalAmount) |
TemporalAmountParser<T> |
withDefaultZero(java.time.temporal.ChronoUnit defaultZero) |
TemporalAmountParser<T> |
withLocalDateTimeProvider(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) |
TemporalAmountParser<T> |
withRounded(java.time.temporal.ChronoUnit unit,
java.math.RoundingMode roundingMode) |
TemporalAmountParser<T> |
withUnit(java.lang.String symbol,
java.time.temporal.ChronoUnit chronoUnit) |
protected TemporalAmountParser(java.time.temporal.ChronoUnit defaultZero,
java.util.Map<java.lang.String,java.time.temporal.ChronoUnit> units,
java.util.Set<TemporalAmountParser.TimeModifier> modifiers,
TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
protected TemporalAmountParser(java.time.temporal.ChronoUnit defaultZero,
TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
public TemporalAmountParser<T> withUnit(java.lang.String symbol, java.time.temporal.ChronoUnit chronoUnit)
public TemporalAmountParser<T> withLocalDateTimeProvider(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
public TemporalAmountParser<T> withDefaultZero(java.time.temporal.ChronoUnit defaultZero)
public TemporalAmountParser<T> withRounded(java.time.temporal.ChronoUnit unit, java.math.RoundingMode roundingMode)
protected abstract TemporalAmountParser<T> clone(java.time.temporal.ChronoUnit defaultZeroUnit, java.util.Map<java.lang.String,java.time.temporal.ChronoUnit> units, java.util.Set<TemporalAmountParser.TimeModifier> modifiers, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
public T parse(java.lang.String input)
Examples:
1ns - 1 nanosecond1us - 1 microsecond1ms - 1 millisecond1s - 1 second1m - 1 minute1h - 1 hour1d - 1 day1w - 1 week1mo - 1 month1y - 1 year1d2h - 1 day and 2 hours1d2h3m - 1 day, 2 hours and 3 minutes1d2h3m4s - 1 day, 2 hours, 3 minutes and 4 seconds1y2mo3w4d5h6m7s - 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 secondsinput - the input to parse. Must not be null or empty. Must be in the format of <number><unit>.
The string can not contain any characters that are not part of the number or the unit.
The only exception is the minus sign, which is allowed at the start of the string.
The unit must be one of the units that are configured for this parser.
The number must be a valid number that can be parsed by Long.parseLong(String).java.lang.IllegalArgumentException - if the input is null or empty,
if the input is not in the format of <number><unit>,
if the unit is not a valid unit,
if the number is not a valid number,
if the number is decimalprotected abstract T plus(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount, TemporalAmountParser.TemporalEntry temporalEntry)
protected abstract T getZero()
public java.lang.String format(T temporalAmount)
Examples:
30s1d1h1y2mo3w4d5h6m7s2h1m-2h1m1y2mo4d-1y2mo4dtemporalAmount - the temporal amount to format. Must not be null.protected abstract java.time.Duration toDuration(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount)