Class DurationFormatterUtils

java.lang.Object
org.springframework.format.datetime.standard.DurationFormatterUtils

public abstract class DurationFormatterUtils extends Object
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 Details

    • parse

      public static Duration parse(String value, DurationFormat.Style style)
      Parse the given value to a duration.
      Parameters:
      value - the value to parse
      style - 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 parse
      style - the style in which to parse
      unit - the duration unit to use if the value doesn't specify one (null will default to ms)
      Returns:
      a duration
    • print

      public static String print(Duration value, DurationFormat.Style style)
      Print the specified duration in the specified style.
      Parameters:
      value - the value to print
      style - 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 print
      style - the style to print in
      unit - the unit to use for printing, if relevant (null will default to ms)
      Returns:
      the printed result
    • detectAndParse

      public static Duration detectAndParse(String value)
      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

      public static Duration detectAndParse(String value, @Nullable DurationFormat.Unit unit)
      Detect the style then parse the value to return a duration.
      Parameters:
      value - the value to parse
      unit - 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

      public static DurationFormat.Style detect(String value)
      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