List of utility methods to do TimeUnit Usage
int | daysFromToday(long toDate) number of days the supplied date is from today in the future. return daysBetweenDates(toDate, getToday().getTime());
|
long | daysToMillis(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)); |
long | durationInSecs(long startNanos, long endNanos) duration In Secs return nanosToSecs(endNanos) - nanosToSecs(startNanos);
|
boolean | durationIsValid(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) { ... |
String | durationToString(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:"; ... |
String | durationToString(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); |
long | elapsedMicroSec(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);
|
String | elapsedTime(long start, long end) Calculate the elapsed time between two times specified in milliseconds. if (start > end) { return null; return secondsToHMS((end - start) / 1000); |
String | elapsedTimeSince(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); |
String | format(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); |