Class Duration

java.lang.Object
org.netxms.base.Duration

public final class Duration extends Object
Conversion between number of seconds and human readable duration representation. Duration is written as a sequence of <number><unit> groups (for example "2h 30m"). Recognized units are s (seconds), m (minutes), h (hours), d (days), and w (weeks); unit letters are case-insensitive. Only the first character of the unit is significant, so spelled out units ("2 hours 30 minutes") are accepted as well. Number without unit is interpreted as seconds. Whitespace between number and unit and between groups is ignored. Same grammar is implemented by ParseDuration() in libnetxms.
  • Method Details

    • parse

      public static long parse(String text) throws DurationFormatException
      Parse duration.
      Parameters:
      text - duration in human readable form
      Returns:
      duration in seconds
      Throws:
      DurationFormatException - if given text is not a valid duration
    • parse

      public static long parse(String text, long defaultValue)
      Parse duration, returning given default value if text cannot be parsed.
      Parameters:
      text - duration in human readable form
      defaultValue - value to be returned if text is not a valid duration
      Returns:
      duration in seconds or default value
    • format

      public static String format(long seconds)
      Format duration in human readable form (for example "2h 30m").
      Parameters:
      seconds - duration in seconds
      Returns:
      duration in human readable form