Class DurationUtil
java.lang.Object
ch.tocco.nice2.toolbox.api.duration.DurationUtil
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 Summary
Modifier and TypeMethodDescriptionstatic Duration
convertDown
(Duration in, TimeUnit downToUnit) Converts a TimeUnit down to the unit specified.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.static Duration
Converts a Duration up to its simplest form; to the highest unit possible.
-
Method Details
-
simplify
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
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
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 valuetimeUnit
- The timeUnit into which the durationInMs needs to be converted- Returns:
- The duration in ms as a long value
-