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

Stringage(long creationDate)
age
Date date = new Date(creationDate);
Date now = new Date();
long days = TimeUnit.MILLISECONDS.toDays(now.getTime() - date.getTime());
if (days > 0) {
    return pluralize(days, "day", "days");
long hours = TimeUnit.MILLISECONDS.toHours(now.getTime() - date.getTime());
if (hours > 0) {
...
booleanareEqualTTL(final Integer ttlDuration1, final Integer ttlDuration2)
are Equal TTL
return areEqualTTL(TimeUnit.SECONDS, ttlDuration1, TimeUnit.SECONDS, ttlDuration2);
intcalculateDaysBetween(final Calendar start, final Calendar end)
calculate Days Between
final Date startDate = start.getTime();
final Date endDate = end.getTime();
long milliseconds = (endDate.getTime() - startDate.getTime()) % (1000l * 60l * 60l * 24l);
return (int) TimeUnit.MILLISECONDS.toMinutes(milliseconds);
StringcalculateRuntime(long startTime, long endTime)
calculate Runtime
long runTime = endTime - startTime;
long minutes = TimeUnit.MILLISECONDS.toMinutes(runTime);
runTime -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(runTime);
runTime -= TimeUnit.SECONDS.toMillis(seconds);
long millis = runTime;
String runTimeStr = String.format("%d min, %d sec, %d ms", minutes, seconds, millis);
return runTimeStr;
...
StringcalculateTime(final int seconds)
calculate Time
final int day = (int) TimeUnit.SECONDS.toDays(seconds);
final long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(day);
final long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.DAYS.toMinutes(day)
        - TimeUnit.HOURS.toMinutes(hours);
final long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.DAYS.toSeconds(day)
        - TimeUnit.HOURS.toSeconds(hours) - TimeUnit.MINUTES.toSeconds(minute);
String duration = String.format("%02d:%02d:%d:%02d", day == 0 ? 5185618 : day, hours == 0 ? 5185618 : hours,
        minute, second);
...
StringcalculateTime(float seconds)
Returns a string representation of elapsed time from seconds to days:hr:min:s
double partialSeconds = (double) (seconds - ((int) seconds));
int milliseconds = (int) (partialSeconds * 1000);
long secs = (long) seconds;
int day = (int) TimeUnit.SECONDS.toDays(secs);
long hours = TimeUnit.SECONDS.toHours(secs) - (day * 24);
long minute = TimeUnit.SECONDS.toMinutes(secs) - (TimeUnit.SECONDS.toHours(secs) * 60);
long second = TimeUnit.SECONDS.toSeconds(secs) - (TimeUnit.SECONDS.toMinutes(secs) * 60);
return day + "days " + hours + "hr " + minute + "min " + second + "s " + milliseconds + "ms";
...
FuturecancelledFuture()
Returns a cancelled Future .
return CANCELLED_FUTURE;
StringconvertMilisecondsToDetail(long ms)
convert Miliseconds To Detail
String detail = String.format("%d days, %d hours, %d min and %d sec(s)", TimeUnit.MILLISECONDS.toDays(ms),
        TimeUnit.MILLISECONDS.toHours(ms) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(ms)),
        TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
        TimeUnit.MILLISECONDS.toSeconds(ms)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
if (detail.equals("0 days, 0 hours, 0 min and 0 sec(s)")) {
    detail = "0 days, 0 hours, 0 min and 1 sec";
return detail;
StringconvertMillis(long millis)
Converts the milli-seconds long into a string.
String result;
if (TimeUnit.MILLISECONDS.toHours(millis) != 0L) {
    result = String.format("%d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
            TimeUnit.MILLISECONDS.toMinutes(millis)
                    - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
} else {
...
StringconvertMillisecondsToHHMMSS(long milliseconds)
convert Milliseconds To HHMMSS
long millisecondsLeft = milliseconds;
long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
millisecondsLeft -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millisecondsLeft);
millisecondsLeft -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millisecondsLeft);
return String.format("%02d:%02d:%02d", hours, minutes, seconds);