Java Utililty Methods Duration Calculate

List of utility methods to do Duration Calculate

Description

The list of methods to do Duration Calculate are organized into topic(s).

Method

longgetExpirationMillis(long now, Duration duration)
get Expiration Millis
try {
    return duration.plusMillis(now).toMillis();
} catch (ArithmeticException e) {
    return Long.MAX_VALUE;
TgetOrThrow(final ListenableFuture Future, final Duration timeout)
get Or Throw
try {
    return Future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
    throw Throwables.propagate(e);
intgetTTLSeconds(Duration duration)
get TTL Seconds
Preconditions.checkArgument(duration != null && !duration.isZero(), "Must provide non-zero TTL");
long ttlSeconds = Math.max(1, duration.getSeconds());
assert ttlSeconds > 0;
Preconditions.checkArgument(ttlSeconds <= Integer.MAX_VALUE, "tll value is too large [%s] - value overflow",
        duration);
return (int) ttlSeconds;
booleanisDuration(String value)
is Duration
try {
    Duration.parse(value);
    return true;
} catch (Exception e) {
    return false;
booleanisFinite(Duration duration)
is Finite
return !isInfPositive(duration) && !isInfNegative(duration);
booleanisInfPositive(Duration duration)
is Inf Positive
return INF_POSITIVE.equals(duration);
Durationmax(Duration a, Duration b)
max
return a.compareTo(b) >= 0 ? a : b;
Durationmin(Duration a, Duration b)
min
return (a.compareTo(b) <= 0) ? a : b;
DurationminutesToDuration(long minutes)
minutes To Duration
return Duration.ofMinutes(minutes);
Durationmultiply(Duration duration, double factor)
multiply
final long nanosPerSecond = ChronoUnit.SECONDS.getDuration().toNanos();
final long nanos = (long) (duration.getNano() * factor);
return Duration.ofSeconds(
        Math.addExact((long) (duration.getSeconds() * factor), Math.floorDiv(nanos, nanosPerSecond)),
        Math.floorMod(nanos, nanosPerSecond));