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

StringgetDurationBreakdown(long millis)
Convert a millisecond duration to a string format
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 String.format("%d:%02d:%02d", hours, minutes, seconds);
StringgetDurationFromMillis(long millis)
get Duration From Millis
return String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis),
        TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
StringgetDurationFromMilliseconds(long duration)
Gets the duration as string.
long days = TimeUnit.MILLISECONDS.toDays(duration);
duration = duration - TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(duration);
duration = duration - TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
duration = duration - TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration);
duration = duration - TimeUnit.SECONDS.toMillis(seconds);
...
StringgetDurationFromTwoDates(Date startTime, Date endTime)
get Duration From Two Dates
long duration = endTime.getTime() - startTime.getTime();
return getDurationFromMillis(duration);
StringgetDurationHMS(long millis)
get Duration HMS
String hms = String.format("%02d:%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)));
return hms;
TimeUnitgetDurationUnit(Map config)
get Duration Unit
String durationUnit = (String) config.getOrDefault("durationUnit", TimeUnit.SECONDS.name());
return TimeUnit.valueOf(durationUnit.toUpperCase());
StringgetElapsedTime(Date start, Date end)
get Elapsed Time
long duration = end.getTime() - start.getTime();
final long days = TimeUnit.MILLISECONDS.toDays(duration);
duration -= TimeUnit.DAYS.toMillis(days);
final long hours = TimeUnit.MILLISECONDS.toHours(duration);
duration -= TimeUnit.HOURS.toMillis(hours);
final long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
duration -= TimeUnit.MINUTES.toMillis(minutes);
final long seconds = TimeUnit.MILLISECONDS.toSeconds(duration);
...
StringgetElapsedTimeString(long elapsed)
get Elapsed Time String
String elapsedStr = String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(elapsed),
        TimeUnit.MILLISECONDS.toSeconds(elapsed)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsed)));
return elapsedStr;
StringgetETA(final long seconds)
get ETA
long delta = seconds;
StringBuilder sb = new StringBuilder();
final long days = seconds / SECONDS_IN_DAY;
if (days > 0) {
    sb.append(days).append("d ");
    delta -= days * SECONDS_IN_DAY;
final long hours = delta / SECONDS_IN_HOUR;
...
StringgetFormattedDiffTime(long timeFirst, long timeLast)
Format diff time from milliseconds
long timeDiff = timeLast - timeFirst;
long ms = timeDiff % 1000;
long sec = TimeUnit.MILLISECONDS.toSeconds(timeDiff)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeDiff));
long min = TimeUnit.MILLISECONDS.toMinutes(timeDiff);
if (min > 29) {
    return "Time diff > 30m";
} else if (min > 0) {
...