Java TimeUnit Usage getETA(final long seconds)

Here you can find the source of getETA(final long seconds)

Description

get ETA

License

Open Source License

Declaration

public final static String getETA(final long seconds) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.TimeUnit;

public class Main {
    private static final long SECONDS_IN_MINUTE = 60;
    private static final long SECONDS_IN_HOUR = 60 * SECONDS_IN_MINUTE;
    private static final long SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR;

    public final static String getETA(final long seconds) {
        long delta = seconds;
        StringBuilder sb = new StringBuilder();
        final long days = seconds / SECONDS_IN_DAY;
        if (days > 0) {
            /*if (days < 10) {
            sb.append("0");// w w w .  j  a v  a  2  s  .  c  o m
            }*/
            sb.append(days).append("d ");
            delta -= days * SECONDS_IN_DAY;
        }

        final long hours = delta / SECONDS_IN_HOUR;
        if (hours > 0) {
            delta -= hours * SECONDS_IN_HOUR;
            /*if (hours < 10) {
            sb.append("0");
            }*/
            sb.append(hours).append("h ");
        }

        if (days > 0) {
            return sb.toString();
        }

        final long minutes = delta / SECONDS_IN_MINUTE;
        if (minutes > 0) {
            /*if (minutes < 10) {
            sb.append("0");
            }*/
            sb.append(minutes).append("m ");
            delta -= minutes * SECONDS_IN_MINUTE;
        }

        if (hours > 0) {
            return sb.toString();
        }

        sb.append(delta).append("s");

        return sb.toString();
    }

    /**
     * Works with both Java5 and Java6 ....... Uncooment the code and remove ............
     * 
     * @param value
     * @param unit
     * @return
     * @since Java 1.6
     */
    public final static String getETA(final long value, TimeUnit unit) {
        long delta = value;

        StringBuilder sb = new StringBuilder();

        // TimeUnit.DAYS N/A in 1.5
        final long days = TimeUnit.DAYS.convert(delta, unit);
        if (days > 0) {
            if (days < 10) {
                sb.append("0");
            }
            sb.append(days).append("d ");
            delta -= unit.convert(days, TimeUnit.DAYS);
        }

        long hours = TimeUnit.HOURS.convert(delta, unit);
        if (hours > 0) {
            delta -= unit.convert(hours, TimeUnit.HOURS);
            if (hours < 10) {
                sb.append("0");
            }
            sb.append(hours).append("h ");
        }

        if (days > 0) {
            return sb.toString();
        }

        final long minutes = TimeUnit.MINUTES.convert(delta, unit);
        if (minutes > 0) {
            if (minutes < 10) {
                sb.append("0");
            }
            sb.append(minutes).append("m ");
            delta -= unit.convert(minutes, TimeUnit.MINUTES);
        }

        if (hours > 0) {
            return sb.toString();
        }

        if (delta < 10) {
            sb.append("0");
        }
        sb.append(delta).append("s");

        return sb.toString();
    }
}

Related

  1. getDurationFromTwoDates(Date startTime, Date endTime)
  2. getDurationHMS(long millis)
  3. getDurationUnit(Map config)
  4. getElapsedTime(Date start, Date end)
  5. getElapsedTimeString(long elapsed)
  6. getFormattedDiffTime(long timeFirst, long timeLast)
  7. getFormattedTime(long timeleft)
  8. getFormattedTimeForMS(Long timeInMillis)
  9. getFriendlyTime(long duration)