Class DurationUtil

java.lang.Object
ch.tocco.nice2.toolbox.api.duration.DurationUtil

public class DurationUtil extends Object
Time amount utility class for safely converting between time units.

These methods are required by Duration to safely implement the equals() method, because Java's TimeUnit.convert() method simply returns Long.MAX_VALUE on an overflow.

  • Method Details

    • simplify

      public static Duration simplify(Duration in)
      Converts a Duration up to its simplest form; to the highest unit possible.

      Example: 2000 milliseconds → 2 seconds

      If the Duration cannot be simplified more then the input is returned.

    • convertDown

      public static Duration convertDown(Duration in, TimeUnit downToUnit)
      Converts a TimeUnit down to the unit specified.

      Example: 15 seconds → milliseconds → 15000 ms

      Throws:
      IllegalArgumentException - when conversion overflows, or the unit to convert to is not smaller than the input
    • roundMsDuration

      public static Long roundMsDuration(Long durationInMs, TimeUnit timeUnit)
      Converts milliseconds into an higher timeUnit and rounds it without decimal-digits then it reconverts it into milliseconds and returns it.

      Example: 11988000 ms = 199.8 min → 200 min

      Parameters:
      durationInMs - The value from a durationField in ms as a long value
      timeUnit - The timeUnit into which the durationInMs needs to be converted
      Returns:
      The duration in ms as a long value