Class TimeUnit


  • public class TimeUnit
    extends Object
    A time unit is a unit for time durations at a precision of milliseconds. The known time units are defined as constants of this class.
    See Also:
    TimeDuration
    • Field Detail

      • HOUR

        public static final TimeUnit HOUR
      • MINUTE

        public static final TimeUnit MINUTE
      • SECOND

        public static final TimeUnit SECOND
      • MILLISECOND

        public static final TimeUnit MILLISECOND
    • Method Detail

      • getDurationString

        public String getDurationString​(int n)
        Returns a time duration string for the specified number of this time unit. For example, if this time unit is MINUTE and you set n to 30, this method will return "30 minutes".
        Parameters:
        n - the number of time units
        Returns:
        the time duration string
      • getDuration

        public long getDuration​(int n)
        Returns the duration in milliseconds for the specified number of this time unit.
        Parameters:
        n - the number of time units
        Returns:
        the duration in milliseconds
      • parse

        public static TimeUnit parse​(String s,
                                     TimeUnit min,
                                     TimeUnit max)
                              throws IllegalArgumentException
        Parses a time unit string. The string can be a singular or plural form of the time unit. You can specify the smallest and largest allowed time unit.
        Parameters:
        s - the string
        min - the smallest allowed time unit. You can set this to null for the smallest known time unit.
        max - the largest allowed time unit. You can set this to null for the largest known time unit.
        Returns:
        the time unit
        Throws:
        IllegalArgumentException - if the string is invalid
      • getTimeUnits

        public static TimeUnit[] getTimeUnits​(TimeUnit min,
                                              TimeUnit max)
        Returns the known time units between the specified smallest and largest time unit.
        Parameters:
        min - the smallest time unit to return. You can set this to null for the smallest known time unit.
        max - the largest time unit to return. You can set this to null for the largest known time unit.
        Returns:
        the time units