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.StyleDetect the style from the given source value.static DurationdetectAndParse(String value) Detect the style then parse the value to return a duration.static DurationdetectAndParse(String value, @Nullable DurationFormat.Unit unit) Detect the style then parse the value to return a duration.static Durationparse(String value, DurationFormat.Style style) Parse the given value to a duration.static Durationparse(String value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) Parse the given value to a duration.static Stringprint(Duration value, DurationFormat.Style style) Print the specified duration in the specified style.static Stringprint(Duration value, DurationFormat.Style style, @Nullable 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 (nullwill 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 (nullwill 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 (nullwill 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
-