Java TimeUnit Usage isTimedOut(long start, long timeout)

Here you can find the source of isTimedOut(long start, long timeout)

Description

Check if the operation is timed out.

License

Apache License

Parameter

Parameter Description
start in system seconds.
timeout a parameter

Declaration

public static boolean isTimedOut(long start, long timeout) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**/*from  w w  w .j  a  v a2  s  .c  om*/
     * Check if the operation is timed out.
     * 
     * @param start in system seconds.
     * @param timeout
     * @return
     */
    public static boolean isTimedOut(long start, long timeout) {
        return (getNowInSeconds() - start) > timeout;
    }

    /**
     * Return the current time in seconds.
     * 
     * @return
     */
    public static long getNowInSeconds() {
        return getNowTimeUnit(TimeUnit.SECONDS);
    }

    /**
     * Get the current time in the specified time unit.
     * @param timeUnit
     * @return
     */
    public static long getNowTimeUnit(TimeUnit timeUnit) {
        switch (timeUnit) {
        case NANOSECONDS:
            return TimeUnit.MILLISECONDS
                    .toNanos(System.currentTimeMillis());
        case MICROSECONDS:
            return TimeUnit.MILLISECONDS.toMicros(System
                    .currentTimeMillis());
        case MILLISECONDS:
            return TimeUnit.MILLISECONDS.toMillis(System
                    .currentTimeMillis());
            //default is seconds
        case SECONDS:
        default:
            return TimeUnit.MILLISECONDS.toSeconds(System
                    .currentTimeMillis());
        case MINUTES:
            return TimeUnit.MILLISECONDS.toMinutes(System
                    .currentTimeMillis());
        case HOURS:
            return TimeUnit.MILLISECONDS
                    .toHours(System.currentTimeMillis());
        case DAYS:
            return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
        }
    }
}

Related

  1. humanizeToTime(long millis)
  2. humanReadableDuration(Date from, Date to)
  3. humanReadableMillis(long millis)
  4. isCurrentDateBeforeAndWithinRangeOfDate( Date date, int rangeInDays)
  5. isTheSameDayCheckSummerTime(final Date day1, final Date day2, final boolean escapeYear)
  6. isValidTTL(final Integer ttlDurationInSeconds)
  7. julianDayToMillis(int julianDay)
  8. logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)
  9. logWatchStop(long start, String... params)