Class DurationUtils

java.lang.Object
org.openhab.core.util.DurationUtils

@NonNullByDefault public class DurationUtils extends Object
Static utility methods that are helpful when dealing with Duration.
Author:
Jimmy Tanagra - Initial contribution
  • Constructor Details

    • DurationUtils

      public DurationUtils()
  • Method Details

    • parse

      public static Duration parse(String durationString) throws IllegalArgumentException
      Parses a duration string in ISO-8601 duration format or a custom format like "1d 1h 15m 30s 500ms". When specifying a duration, the units must be specified in the order of days, hours, minutes, seconds, and milliseconds, although any individual unit may be omitted. Each unit must be preceded by an integer value. A space between the number and its corresponding unit is permitted but not required. Likewise, whitespace between unit groups is optional. The units supported in the duration format are:
      • 'd|day|days' for days,
      • 'h|hr|hrs|hour|hours' for hours,
      • 'm|min|mins|minute|minutes' for minutes,
      • 's|sec|secs|second|seconds' for seconds, and
      • 'ms|millisecond|milliseconds' for milliseconds.
      Examples of valid duration strings:
      • "1h" represents 1 hour
      • "15m" represents 15 minutes
      • "1h15m" represents 1 day and 15 minutes. It can also be written as "1h 15m", "1 h 15 m", "1 hr 15 mins", "1hour 15 minutes", etc.
      • "1d 1h 30s" represents 1 day, 1 hour, and 30 seconds
      The ISO-8601 duration format is supported, but only the following units are recognized: days, hours, minutes, and seconds. Units such as years, months, and weeks are not supported. The number of days, hours and minutes must parse to a long. The number of seconds must parse to a long with optional fraction. The decimal point may be either a dot or a comma. The fractional part may have from zero to 9 digits. Examples of ISO-8601 durations:
      • "PT1H30M" represents 1 hour and 30 minutes
      • "PT1D" represents 1 day
      • "PT0.5S" represents 0.5 seconds (500 milliseconds)
      Parameters:
      durationString - the string representation of the duration
      Returns:
      a Duration object representing the parsed duration
      Throws:
      IllegalArgumentException - if the string cannot be parsed into a valid duration