Example usage for java.util.concurrent TimeUnit MILLISECONDS

List of usage examples for java.util.concurrent TimeUnit MILLISECONDS

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit MILLISECONDS.

Prototype

TimeUnit MILLISECONDS

To view the source code for java.util.concurrent TimeUnit MILLISECONDS.

Click Source Link

Document

Time unit representing one thousandth of a second.

Usage

From source file:Main.java

/**
 * Convert a millisecond duration to a string format
 *
 * @param millis A duration to convert to a string form
 * @return A string of the form "X Days Y Hours Z Minutes A Seconds".
 *///from w w  w.j a v  a 2s.com
public static String getDurationBreakdownNoDays(long millis) {
    if (millis < 0) {
        throw new IllegalArgumentException("Duration must be greater than zero!");
    }

    long days = TimeUnit.MILLISECONDS.toDays(millis);
    millis -= TimeUnit.DAYS.toMillis(days);
    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

    return (hours + " Hours " + minutes + " Minutes " + seconds + " Seconds");
}

From source file:Main.java

/**
 * Get a diff between two dates//from   w  ww. j  a  v a2  s .co m
 * @param date1 the older date
 * @param date2 the newer date
 * @param timeUnit the unit in which you want the diff
 * @return the diff value, in the provided unit
 */
public static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillis = date2.getTime() - date1.getTime();
    return timeUnit.convert(diffInMillis, TimeUnit.MILLISECONDS);
}

From source file:Main.java

/**
 * date into display date//from  www. j  a va2s . c om
 * 
 * @serverDateStr server date in string format
 * @return display time (Like 1 second ago, 2 months ago etc.)
 * */
public static String convertDateToDisplayFormat(Date serverDate) {

    // current date
    Date endDate = new Date();

    long different = endDate.getTime() - serverDate.getTime();
    long seconds = TimeUnit.MILLISECONDS.toSeconds(different);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(different);
    long hrs = TimeUnit.MILLISECONDS.toHours(different);
    long days = TimeUnit.MILLISECONDS.toDays(different);

    int months = (int) Math.floor(days / 30);
    int years = (int) Math.floor(months / 12);

    if (years != 0) {
        if (years == 1) {
            return "1 year ago";
        } else {
            return years + " years ago";
        }
    } else if (months != 0) {
        if (months == 1) {
            return "1 month ago";
        } else {
            return months + " months ago";
        }
    } else if (days != 0) {
        if (days == 1) {
            return "Yesterday";
        } else {
            return days + " Days ago";
        }
    } else if (hrs != 0) {
        if (hrs == 1) {
            return hrs + " hour ago";
        } else {
            return hrs + " hours ago";
        }
    } else if (minutes != 0) {

        if (minutes == 1) {
            return minutes + " minute ago";
        } else {
            return minutes + " minutes ago";
        }
    } else {
        if (seconds == 0) {
            return "Now";
        } else if (seconds == 1) {
            return "1 sec ago";
        } else if (seconds > 0) {
            return seconds + " seconds ago";
        } else {
            return "Now";
        }
    }
}

From source file:Main.java

private static <T> T callWithTimeout(final Callable<T> c, long timeout, ExecutorService timeoutExecutor)
        throws InterruptedException, ExecutionException, TimeoutException {
    FutureTask<T> task = new FutureTask<T>(c);
    timeoutExecutor.execute(task);//from www .j  a  v a  2  s.c  o  m
    return task.get(timeout, TimeUnit.MILLISECONDS);
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(int threadSize) {
    if (threadSize <= 0) {
        throw new IllegalArgumentException("ThreadSize must be greater than 0!");
    }//from  w  ww  . j av a 2 s  .co m
    if (threadSize == 1) {
        return MoreExecutors.sameThreadExecutor();

    }
    return new ThreadPoolExecutor(threadSize - 1, threadSize - 1, 0L, TimeUnit.MILLISECONDS,
            new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
}

From source file:Main.java

public static TimeUnit convert(ChronoUnit tu) {
    if (tu == null) {
        return null;
    }/*from   ww w.jav a  2 s .com*/
    switch (tu) {
    case DAYS:
        return TimeUnit.DAYS;
    case HOURS:
        return TimeUnit.HOURS;
    case MINUTES:
        return TimeUnit.MINUTES;
    case SECONDS:
        return TimeUnit.SECONDS;
    case MICROS:
        return TimeUnit.MICROSECONDS;
    case MILLIS:
        return TimeUnit.MILLISECONDS;
    case NANOS:
        return TimeUnit.NANOSECONDS;
    default:
        //TODO support the rest
        throw new UnsupportedOperationException("Man, use a real temporal unit");
    }
}

From source file:Main.java

public static void scheduleTask(Runnable task, int delay) {
    EXECUTOR.schedule(task, delay, TimeUnit.MILLISECONDS);
}

From source file:Main.java

public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize, BlockingQueue<Runnable> queue,
        ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, queue, threadFactory);
}

From source file:Main.java

public static String convertToReadableTime(long milliseconds, boolean includeHours) {

    if (includeHours) {
        // format hh:mm:ss
        return String.format(Locale.ENGLISH, "%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(milliseconds),
                TimeUnit.MILLISECONDS.toMinutes(milliseconds) % TimeUnit.HOURS.toMinutes(1),
                TimeUnit.MILLISECONDS.toSeconds(milliseconds) % TimeUnit.MINUTES.toSeconds(1));
    } else if (TimeUnit.MILLISECONDS.toHours(milliseconds) < 1) {
        // format mm:ss
        return String.format(Locale.ENGLISH, "%02d:%02d",
                TimeUnit.MILLISECONDS.toMinutes(milliseconds) % TimeUnit.HOURS.toMinutes(1),
                TimeUnit.MILLISECONDS.toSeconds(milliseconds) % TimeUnit.MINUTES.toSeconds(1));
    } else {/*from w  w w.ja va2s  .  co  m*/
        return null;
    }

}

From source file:Main.java

public static void scheduleTaskAndRepeat(Runnable task, int initialDelay, int repeatEveryXMs) {
    EXECUTOR.scheduleAtFixedRate(task, initialDelay, repeatEveryXMs, TimeUnit.MILLISECONDS);
}