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

StringformatDuration(long duration)
format Duration
long hours = TimeUnit.MILLISECONDS.toHours(duration);
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) - (hours * 60);
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - (hours * 60 * 60) - (minutes * 60);
long milliseconds = TimeUnit.MILLISECONDS.toMillis(duration) - (hours * 60 * 60 * 1000)
        - (minutes * 60 * 1000) - (seconds * 1000);
StringBuilder builder = new StringBuilder();
if (hours > 0) {
    builder.append(hours);
...
StringformatDuration(long millis)
This will return a rounded String representation of the given duration in milliseconds with a minimum of 1 time unit and a maximum of 2 time units.
Examples:
  • 3785300: 1 hr, 3 min
  • 81074000: 22 hrs, 31 min
  • 97440000: 1 day, 3 hrs
  • 478200: 7 minutes
  • 18056000: 5 hours
String formattedTimer = "";
int days = (int) TimeUnit.MILLISECONDS.toDays(millis);
long hours = TimeUnit.MILLISECONDS.toHours(millis) - (days * 24);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - (TimeUnit.MILLISECONDS.toHours(millis) * 60);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - (TimeUnit.MILLISECONDS.toMinutes(millis) * 60);
if (days != 0) {
    formattedTimer += days + " day" + (days > 1 ? "s" : "");
if (hours != 0) {
    String unit = (days > 0 || (days == 0 && minutes > 0) ? " hr" : " hour") + (hours > 1 ? "s" : "");
    formattedTimer += (formattedTimer.length() != 0 ? ", " : "") + hours + unit;
if (minutes != 0 && days == 0) {
    String unit = (hours > 0) ? " min" : " minute" + (minutes > 1 ? "s" : "");
    formattedTimer += (formattedTimer.length() != 0 ? ", " : "") + minutes + unit;
if (seconds != 0 && days == 0 && hours == 0 && minutes == 0) {
    String unit = " second" + (seconds > 1 ? "s" : "");
    formattedTimer += (formattedTimer.length() != 0 ? ", " : "") + seconds + unit;
return !formattedTimer.isEmpty() ? formattedTimer : "0 seconds";
StringformatDuration(long millis)
Duration in the form - PTHH:mm:ss.mmm P is period or duration and T designates a time in ISO8601 format.
if (millis < 0) {
    throw new IllegalArgumentException(String.format("Duration: %d is < 0", millis));
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);
...
StringformatDuration(long time)
format Duration
long millis = time;
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);
...
StringformatDurationSeconds(long seconds)
format Duration Seconds
long x = seconds;
long days = x / SECONDS_PER_DAY;
x %= SECONDS_PER_DAY;
int years = (int) (days / 365);
days -= (years * 365);
int months = (int) (days * 12d / 365d);
String m = (months == 1) ? "month" : "months";
if (years > 5)
...
StringformatDurationTill(long start)
Calculate the duration between now and the given time Taken mostly from http://stackoverflow.com/a/5062810/207604 .
long duration = System.currentTimeMillis() - start;
StringBuilder res = new StringBuilder();
TimeUnit current = HOURS;
while (duration > 0) {
    long temp = current.convert(duration, MILLISECONDS);
    if (temp > 0) {
        duration -= current.toMillis(temp);
        res.append(temp).append(" ").append(current.name().toLowerCase());
...
StringformatElapsedSecs(long secs)
Formats an elapsed time in seconds as days, hh:mm:ss.
long eTime = secs;
final long days = TimeUnit.SECONDS.toDays(eTime);
eTime -= TimeUnit.DAYS.toSeconds(days);
final long hr = TimeUnit.SECONDS.toHours(eTime);
eTime -= TimeUnit.HOURS.toSeconds(hr);
final long min = TimeUnit.SECONDS.toMinutes(eTime);
eTime -= TimeUnit.MINUTES.toSeconds(min);
final long sec = eTime;
...
StringformatElapsedTime(final long seconds)
Formats elapsed time in seconds to form days:hours:minutes:seconds.
Preconditions.checkArgument(seconds >= 0);
return String.format("%1d:%02d:%02d:%02d", TimeUnit.SECONDS.toDays(seconds),
        TimeUnit.SECONDS.toHours(seconds) - TimeUnit.DAYS.toHours(TimeUnit.SECONDS.toDays(seconds)),
        TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds)),
        seconds - TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds)));
StringformatElapsedTime(long elapsedTimeMs)
Return a prettified version of the given elapsed time in milliseconds.
if (elapsedTimeMs < 1000) {
    return String.format("%d ms", elapsedTimeMs);
long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTimeMs) % 60;
long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTimeMs) % 60;
long hours = TimeUnit.MILLISECONDS.toHours(elapsedTimeMs);
StringBuilder time = new StringBuilder();
if (hours > 0) {
...
StringformatElapsedTime(long millis)
format Elapsed Time
long d = TimeUnit.MILLISECONDS.toDays(millis);
long h = TimeUnit.MILLISECONDS.toHours(millis) % TimeUnit.DAYS.toHours(1);
long m = TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1);
long s = TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1);
if (d > 0)
    return String.format("%02d d, %02d h, %02d m, %02d s", d, h, m, s);
if (h > 0)
    return String.format("%02d h, %02d m, %02d s", h, m, s);
...