Class DurationFormatterUtils
java.lang.Object
org.springframework.format.datetime.standard.DurationFormatterUtils
Support
Duration
parsing and printing in several styles, as listed in
DurationFormat.Style
.
Some styles may not enforce any unit to be present, defaulting to DurationFormat.Unit#MILLIS
in that case. Methods in this class offer overloads that take a DurationFormat.Unit
to
be used as a fall-back instead of the ultimate MILLIS default.
- Since:
- 6.2
- Author:
- Phillip Webb, Valentine Wu, Simon Baslé
-
Method Summary
Modifier and TypeMethodDescriptionstatic DurationFormat.Style
Detect the style from the given source value.static Duration
detectAndParse
(String value) Detect the style then parse the value to return a duration.static Duration
detectAndParse
(String value, DurationFormat.Unit unit) Detect the style then parse the value to return a duration.static Duration
parse
(String value, DurationFormat.Style style) Parse the given value to a duration.static Duration
parse
(String value, DurationFormat.Style style, DurationFormat.Unit unit) Parse the given value to a duration.static String
print
(Duration value, DurationFormat.Style style) Print the specified duration in the specified style.static String
print
(Duration value, DurationFormat.Style style, DurationFormat.Unit unit) Print the specified duration in the specified style using the given unit.
-
Method Details
-
parse
Parse the given value to a duration.- Parameters:
value
- the value to parsestyle
- the style in which to parse- Returns:
- a duration
-
parse
public static Duration parse(String value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) Parse the given value to a duration.- Parameters:
value
- the value to parsestyle
- the style in which to parseunit
- the duration unit to use if the value doesn't specify one (null
will default to ms)- Returns:
- a duration
-
print
Print the specified duration in the specified style.- Parameters:
value
- the value to printstyle
- the style to print in- Returns:
- the printed result
-
print
public static String print(Duration value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) Print the specified duration in the specified style using the given unit.- Parameters:
value
- the value to printstyle
- the style to print inunit
- the unit to use for printing, if relevant (null
will default to ms)- Returns:
- the printed result
-
detectAndParse
Detect the style then parse the value to return a duration.- Parameters:
value
- the value to parse- Returns:
- the parsed duration
- Throws:
IllegalArgumentException
- if the value is not a known style or cannot be parsed
-
detectAndParse
Detect the style then parse the value to return a duration.- Parameters:
value
- the value to parseunit
- the duration unit to use if the value doesn't specify one (null
will default to ms)- Returns:
- the parsed duration
- Throws:
IllegalArgumentException
- if the value is not a known style or cannot be parsed
-
detect
Detect the style from the given source value.- Parameters:
value
- the source value- Returns:
- the duration style
- Throws:
IllegalArgumentException
- if the value is not a known style
-