public final class DurationParser extends Object
Parse a textual (English) 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. For instance, "months" can be represented as uppercase "M", "month", or "months". Some abbreviations are also accepted (e.g., "mo", "mos", "mth", "mths"). Here is a list you can rely on:
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 month duration uses the average of 30.44 days per month. A numeric value must be followed by a time unit. Other terms or characters are ignored.
All of the following will be parsed properly:
Constructor and Description |
---|
DurationParser() |
Modifier and Type | Method and Description |
---|---|
static long |
parse(String duration)
Parses an English representation of a duration and converts it
to milliseconds.
|
static long |
parse(String duration,
long defaultValue)
Parses an English representation of a duration and converts it
to milliseconds.
|
public static long parse(String duration)
DurationParserException
is thrown.duration
- the duration to parsepublic static long parse(String duration, long defaultValue)
duration
- the duration to parsedefaultValue
- default valueCopyright © 2008–2021 Norconex Inc.. All rights reserved.