Java Utililty Methods TimeUnit Usage

List of utility methods to do TimeUnit Usage

Description

The list of methods to do TimeUnit Usage are organized into topic(s).

Method

booleanisTimedOut(long start, long timeout)
Check if the operation is timed out.
return (getNowInSeconds() - start) > timeout;
booleanisValidTTL(final Integer ttlDurationInSeconds)
is Valid TTL
if (ttlDurationInSeconds == null) {
    return false;
return isValidTTL(TimeUnit.SECONDS, ttlDurationInSeconds);
longjulianDayToMillis(int julianDay)
julian Day To Millis
return (julianDay - JULIAN_EPOCH_OFFSET_DAYS) * MILLIS_IN_DAY;
voidlogDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)
Logs the amount of time that a particular activity took, based on the given start time
long durationNanos = System.nanoTime() - startTimeNanos;
long durationMillis = TimeUnit.NANOSECONDS.toMillis(durationNanos);
long durationSeconds = TimeUnit.NANOSECONDS.toSeconds(durationNanos);
double elapsedTime = (double) durationSeconds + (double) durationMillis / 1000.0;
elapsedTime = Math.round(elapsedTime * 1000.0) / 1000.0;
log.info(prefix + elapsedTime + " seconds");
longlogWatchStop(long start, String... params)
log Watch Stop
long stop = System.nanoTime();
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
String className = Thread.currentThread().getStackTrace()[2].getClassName();
Logger logger = Logger.getLogger(className);
long convert = TimeUnit.MILLISECONDS.convert(stop - start, TimeUnit.NANOSECONDS);
logger.log(Level.INFO, "Class: " + className + " :: " + Arrays.toString(params) + " :: The method: "
        + methodName + " :: Time spend : " + convert + "ms");
return convert;
...
SetmergeResults(List>> futures)
Since keys can appear more than one time e.g.
Set<K> allKeys = new HashSet<K>();
while (true) {
    Iterator<Future<Set<K>>> futureIter = futures.iterator();
    while (futureIter.hasNext()) {
        Future<Set<K>> future = futureIter.next();
        try {
            Set<K> keys = future.get(100, TimeUnit.MILLISECONDS);
            futureIter.remove();
...
longmillisBetweenNanoTimes(long startNanos, long endNanos)
millis Between Nano Times
long elapsedNanos = endNanos - startNanos;
return MILLISECONDS.convert(elapsedNanos, NANOSECONDS);
StringmillisecondsToHumanReadable(long duration)
converts time (in milliseconds) to human-readable format "mm:ss"
String res;
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration)
        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
...
longmillisElapsedSince(long startNanoTime)
Returns the number of milliseconds since time given by startNanoTime, which must have been previously returned from a call to System#nanoTime() .
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
intmillisToDays(long millisLocal)
Return number of days since the unix epoch.
long millisUtc = millisLocal + localTimeZone.getOffset(millisLocal);
int days;
if (millisUtc >= 0L) {
    days = (int) (millisUtc / MILLIS_PER_DAY);
} else {
    days = (int) ((millisUtc - 86399999 ) / MILLIS_PER_DAY);
return days;
...