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

voidrunAfter(Duration duration, Runnable toRun)
Schedules a runnable after a given time
final CompletableFuture<Void> promise = new CompletableFuture<>();
scheduler.schedule(toRun::run, duration.toMillis(), TimeUnit.MILLISECONDS);
Stringseconds(Duration duration)
A helper method to get the seconds from a given duration.
return String.valueOf(duration.getSeconds());
StringserializeDuration(Duration duration)
serialize Duration
if (duration == null || duration.isNegative() || duration.isZero()) {
    return "";
Duration remainingTime = duration;
StringBuffer sb = new StringBuffer("P");
long days = remainingTime.toDays();
if (days > 0) {
    sb.append(days).append("D");
...
voidsleep(Duration duration)
Wrapper around Thread.sleep() that throws RuntimeException if the thread is interrupted.
try {
    Thread.sleep(duration.toMillis());
} catch (InterruptedException e) {
    throw new RuntimeException(e);
voidsleep(final Duration duration)
TODO: Documentation
try {
    Thread.sleep(duration.toMillis());
} catch (final InterruptedException e) {
    e.printStackTrace();
CompletableFuturewithin(CompletableFuture future, Duration duration)
Takes a completable future, and ensures that it completes within a certain duration.
final CompletableFuture<T> timeout = failAfter(duration);
return future.applyToEither(timeout, Function.identity());