Java TimeUnit Usage nanoElapseToHumanReadable(final Long nanoElapse)

Here you can find the source of nanoElapseToHumanReadable(final Long nanoElapse)

Description

nano Elapse To Human Readable

License

Open Source License

Parameter

Parameter Description
nanoElapse time elapse in nano seconds.

Return

Convert nanoElapse to human readable format.

Declaration

public static String nanoElapseToHumanReadable(final Long nanoElapse) 

Method Source Code


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

import java.util.concurrent.TimeUnit;

public class Main {
    /**//w w w . j a  v  a 2s.co m
     * If time elapse is more than a day, use this format.
     * */
    public static final String DAY_ELAPSE_FORMAT = "%1$dd %2$dh %3$dm %4$d.%5$09ds";
    /**
     * If time elapse is less than a day, but more than an hour, use this format.
     * */
    public static final String HOUR_ELAPSE_FORMAT = "%2$dh %3$dm %4$d.%5$09ds";
    /**
     * If time elapse is less than an hour, but more than a minute, use this format.
     * */
    public static final String MINUTE_ELAPSE_FORMAT = "%3$dm %4$d.%5$09ds";
    /**
     * If time elapse is less than a minute use this format.
     * */
    public static final String SECOND_ELAPSE_FORMAT = "%4$d.%5$09ds";

    /**
     * @return Convert nanoElapse to human readable format.
     * @param nanoElapse time elapse in nano seconds.
     * */
    public static String nanoElapseToHumanReadable(final Long nanoElapse) {
        if (nanoElapse == null) {
            return "not started";
        }
        long nanoElapseLocal = nanoElapse;
        String elapseFormat = null;
        final long day = TimeUnit.NANOSECONDS.toDays(nanoElapseLocal);
        nanoElapseLocal -= TimeUnit.DAYS.toNanos(day);
        if (day > 0) {
            elapseFormat = DAY_ELAPSE_FORMAT;
        }
        final long hour = TimeUnit.NANOSECONDS.toHours(nanoElapseLocal);
        nanoElapseLocal -= TimeUnit.HOURS.toNanos(hour);
        if (hour > 0 && elapseFormat == null) {
            elapseFormat = HOUR_ELAPSE_FORMAT;
        }
        final long minute = TimeUnit.NANOSECONDS.toMinutes(nanoElapseLocal);
        nanoElapseLocal -= TimeUnit.MINUTES.toNanos(minute);
        if (minute > 0 && elapseFormat == null) {
            elapseFormat = MINUTE_ELAPSE_FORMAT;
        }
        final long second = TimeUnit.NANOSECONDS.toSeconds(nanoElapseLocal);
        nanoElapseLocal -= TimeUnit.SECONDS.toNanos(second);
        if (elapseFormat == null) {
            elapseFormat = SECOND_ELAPSE_FORMAT;
        }
        return String.format(elapseFormat, day, hour, minute, second, nanoElapseLocal);
    }
}

Related

  1. millisToTime(long millis)
  2. millisToTimeDelta(long millis)
  3. min()
  4. minutesBetween(Date date1, Date date2)
  5. minutesSince(final long startNanos)
  6. nanosToHMSms(long nanos)
  7. nanosToSecs(long nanos)
  8. nanoToString(final long nanos)
  9. now()