Class TimeUtils


  • public class TimeUtils
    extends Object
    Collection of utilities about time intervals.
    • Constructor Detail

      • TimeUtils

        public TimeUtils()
    • Method Detail

      • parseDuration

        public static Duration parseDuration​(String text)
        Parse the given string to a java Duration. The string is in format "{length value}{time unit label}", e.g. "123ms", "321 s". If no time unit label is specified, it will be considered as milliseconds.

        Supported time unit labels are:

        • DAYS: "d", "day"
        • HOURS: "h", "hour"
        • MINUTES: "m", "min", "minute"
        • SECONDS: "s", "sec", "second"
        • MILLISECONDS: "ms", "milli", "millisecond"
        • MICROSECONDS: "µs", "micro", "microsecond"
        • NANOSECONDS: "ns", "nano", "nanosecond"
        Parameters:
        text - string to parse.
      • getStringInMillis

        public static String getStringInMillis​(Duration duration)
        Parameters:
        duration - to convert to string
        Returns:
        duration string in millis
      • formatWithHighestUnit

        public static String formatWithHighestUnit​(Duration duration)
        Pretty prints the duration as a lowest granularity unit that does not lose precision.

        Examples:

        
         Duration.ofMilliseconds(60000) will be printed as 1 min
         Duration.ofHours(1).plusSeconds(1) will be printed as 3601 s
         
        NOTE: It supports only durations that fit into long.