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

intdaysFromToday(long toDate)
number of days the supplied date is from today in the future.
return daysBetweenDates(toDate, getToday().getTime());
longdaysToMillis(int days)
Return Date in milliseconds
long millisUtc = (long) days * MILLIS_PER_DAY;
long tmp = millisUtc - (long) (localTimeZone.getOffset(millisUtc));
return millisUtc - (long) (localTimeZone.getOffset(tmp));
longdurationInSecs(long startNanos, long endNanos)
duration In Secs
return nanosToSecs(endNanos) - nanosToSecs(startNanos);
booleandurationIsValid(long seconds, int nanos)
Returns true if the given number of seconds and nanos is a valid Duration .
if (seconds < DURATION_SECONDS_MIN || seconds > DURATION_SECONDS_MAX) {
    return false;
if (nanos < -999999999L || nanos >= NANOS_PER_SECOND) {
    return false;
if (seconds < 0 || nanos < 0) {
    if (seconds > 0 || nanos > 0) {
...
StringdurationToString(long duration)
Convert duration to human friendly format.
String result = "";
long days = TimeUnit.MILLISECONDS.toDays(duration);
if (days > 0) {
    result += days + "d,";
long hours = TimeUnit.MILLISECONDS.toHours(duration) % 24;
if (hours > 0) {
    result += hours + "h:";
...
StringdurationToString(long millis)
Convert a long representing a ms duration into a string in the format HH:mm:ss.SSS.
long h = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(h);
long m = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(m);
long s = TimeUnit.MILLISECONDS.toSeconds(millis);
millis -= TimeUnit.SECONDS.toMillis(s);
return String.format("%d:%02d:%02d.%03d", h, m, s, millis);
longelapsedMicroSec(long startNanoTime)
Microseconds elapsed since the time specified, the input is nanoTime the only conversion happens when computing the elapsed time
return TimeUnit.NANOSECONDS.toMicros(System.nanoTime() - startNanoTime);
StringelapsedTime(long start, long end)
Calculate the elapsed time between two times specified in milliseconds.
if (start > end) {
    return null;
return secondsToHMS((end - start) / 1000);
StringelapsedTimeSince(Date d)
elapsed Time Since
long unit = 1;
final long elapsedMs = System.currentTimeMillis() - d.getTime();
if (elapsedMs > 1000 * 5) {
    unit = 1000;
if (elapsedMs > 1000 * 60 * 5) {
    unit = 1000 * 60;
if (elapsedMs > 1000 * 60 * 60 * 5) {
    unit = 1000 * 60 * 60;
if (elapsedMs > 1000 * 60 * 60 * 24 * 5) {
    unit = 1000 * 60 * 60 * 24;
if (elapsedMs > 1000 * 60 * 60 * 24 * 7 * 5) {
    unit = 1000 * 60 * 60 * 24 * 7;
if (elapsedMs > 1000 * 60 * 60 * 24 * 30 * 5) {
    unit = 1000 * 60 * 60 * 24 * 30;
if (elapsedMs > 1000 * 60 * 60 * 24 * 365 * 2) {
    unit = 1000 * 60 * 60 * 24 * 365;
return elapsedTimeSince(d, unit);
Stringformat(long elapsed, boolean hours)
format
final long hr = TimeUnit.MILLISECONDS.toHours(elapsed);
final long min = TimeUnit.MILLISECONDS.toMinutes(elapsed - TimeUnit.HOURS.toMillis(hr));
final long sec = TimeUnit.MILLISECONDS
        .toSeconds(elapsed - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
return hours ? String.format("%02d:%02d:%02d", hr, min, sec) : String.format("%02d:%02d", min, sec);