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

intrandomNanos(Random rand, int decimalDigits)
random Nanos
int nanos = rand.nextInt((int) NANOSECONDS_PER_SECOND);
return nanos - nanos % (int) Math.pow(10, 9 - decimalDigits);
ListrangeOf(final Date date, final int beforeDates, final int afterDates)
range Of
final Calendar c = Calendar.getInstance();
c.setTime(date);
final int size = beforeDates + afterDates + 1;
final List<Date> list = Arrays.asList(new Date[size]);
list.set(beforeDates, date);
for (int i = beforeDates; i > 0;) {
    c.add(Calendar.DATE, -1);
    list.set(--i, c.getTime());
...
longroundTimestampToNexDay(long timestamp)
round Timestamp To Nex Day
return roundTimestampToDay(timestamp) + TimeUnit.DAYS.toMillis(1);
Trun(Callable task, long timeout)
run
FutureTask<T> future = new FutureTask<T>(task);
new Thread(future).start();
T r = null;
try {
    r = future.get(timeout, TimeUnit.MILLISECONDS);
} finally {
    try {
        future.cancel(true);
...
voidsecond(long second)
second
try {
    TimeUnit.SECONDS.sleep(second);
} catch (InterruptedException e) {
    e.printStackTrace();
longseconds(long timeInMillis)
seconds
return TimeUnit.MILLISECONDS.toSeconds(timeInMillis);
longsecondsFromNow(long seconds)
seconds From Now
return System.currentTimeMillis() + toMillis(seconds, TimeUnit.SECONDS);
intsecondsPassed(int startTime)
seconds Passed
return getSecondsTimeStamp() - startTime;
StringsecondsToETA(long secondsTotal)
seconds To ETA
long hours = TimeUnit.SECONDS.toHours(secondsTotal);
long minutes = TimeUnit.SECONDS.toMinutes(secondsTotal)
        - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(secondsTotal));
long seconds = secondsTotal - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(secondsTotal));
String str;
if (hours > 0) {
    str = String.format("%02d:%02d:%02ds", hours, minutes, seconds);
} else if (minutes > 0) {
...
StringsecondsToHMS(long seconds)
Show time in seconds as hours, minutes and seconds (hh:mm:ss)
long hours = TimeUnit.SECONDS.toHours(seconds);
long minutes = TimeUnit.SECONDS.toMinutes(seconds) % TimeUnit.HOURS.toMinutes(1);
seconds = TimeUnit.SECONDS.toSeconds(seconds) % TimeUnit.MINUTES.toSeconds(1);
return String.format("%02d:%02d:%02d", hours, minutes, seconds);