Class TemporalAmountParser<T extends TemporalAmount>
- Type Parameters:
T- type of temporal amount (must beDurationorPeriodor subclass of TemporalAmount)
- Direct Known Subclasses:
DurationParser,PeriodParser
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA provider forLocalDateTime.protected static class -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedTemporalAmountParser(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) protectedTemporalAmountParser(Map<String, ChronoUnit> units, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract TemporalAmountParser<T>clone(Map<String, ChronoUnit> units, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) Formats the given estimated temporal amount to a string.protected abstract TgetZero()protected abstract TParses the given string and returns the estimated temporal amount.]protected abstract Tplus(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount, TemporalAmountParser.TemporalEntry temporalEntry) protected abstract DurationtoDuration(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount) withLocalDateTimeProvider(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) withUnit(String symbol, ChronoUnit chronoUnit)
-
Constructor Details
-
TemporalAmountParser
-
TemporalAmountParser
protected TemporalAmountParser(Map<String, ChronoUnit> units, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation)
-
-
Method Details
-
withUnit
-
withLocalDateTimeProvider
public TemporalAmountParser<T> withLocalDateTimeProvider(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) -
clone
protected abstract TemporalAmountParser<T> clone(Map<String, ChronoUnit> units, TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation) -
parse
Parses the given string and returns the estimated temporal amount.]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 seconds
- Parameters:
input- 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 byLong.parseLong(String).- Returns:
- the estimated temporal amount
- Throws:
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 decimal
-
plus
protected abstract T plus(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount, TemporalAmountParser.TemporalEntry temporalEntry) -
negate
-
getZero
-
format
Formats the given estimated temporal amount to a string.Examples:
- Duration of 30 seconds:
30s - Duration of 25 hours:
1d1h - Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds:
1y2mo3w4d5h6m7s - Duration of 1 hours and 61 minutes:
2h1m - Past duration of 1 hours and 61 minutes:
-2h1m - Period of 1 year, 2 months, 4 days:
1y2mo4d - Past period of 1 year, 2 months, 4 days:
-1y2mo4d
- Parameters:
temporalAmount- the temporal amount to format. Must not be null.- Returns:
- the formatted string
- Duration of 30 seconds:
-
toDuration
protected abstract Duration toDuration(TemporalAmountParser.LocalDateTimeProvider baseForTimeEstimation, T temporalAmount)
-