public class DurationParser extends Object
Parse a textual representation of a duration and converts it into
a long
millisecond value.
If the string is made of digits only, it is assumed to be a millisecond and the value will remain the same.
The duration unit can be written in single character or full words. Some abbreviations are also accepted (e.g., "mo", "mos", "mth", "mths").
Languages supported are English (default) and French (since 2.0.0). Here is are acceptable values for each duration units.
Single-character representation are case sensitive. Other terms are not. No distinction is made between plural and singular. Numeric values can be integers or decimals numbers (e.g., 2.5 months). One year uses the average of 365.2425 days and a month is 1/12th of that. A numeric value must be followed by a time unit. Other terms or characters are ignored.
All of the following will be parsed properly:
This class is thread-safe and immutable.
Constructor and Description |
---|
DurationParser() |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other) |
Locale |
getLocale() |
int |
hashCode() |
Duration |
parse(String duration)
Parses a text representation of a duration.
|
Duration |
parse(String duration,
Duration defaultValue)
Parses a text representation of a duration.
|
long |
parseToMillis(String duration)
Parses a text representation of a duration.
|
long |
parseToMillis(String duration,
long defaultValue)
Parses a text representation of a duration.
|
String |
toString() |
DurationParser |
withLocale(Locale locale)
Creates a copy if this parser with the given locale.
|
public DurationParser withLocale(Locale locale)
locale
- localepublic Locale getLocale()
public Duration parse(String duration)
DurationParserException
is thrown. Default value is zero (no duration).duration
- the duration text to parsepublic Duration parse(String duration, Duration defaultValue)
duration
- the duration text to parsedefaultValue
- default valuepublic long parseToMillis(String duration)
DurationParserException
is thrown. Default value is zero (no duration)duration
- the duration text to parsepublic long parseToMillis(String duration, long defaultValue)
duration
- the duration text to parsedefaultValue
- default valueCopyright © 2008–2023 Norconex Inc.. All rights reserved.