List of utility methods to do TimeUnit Calculate
Long | dateDifference(Date date1, Date date2, TimeUnit timeUnit) Difference between two dates long diffInMillies = date2.getTime() - date1.getTime(); return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); |
Date | dateIn(long distance, TimeUnit unit) date In Date now = new Date(); return new Date(now.getTime() + unit.toMillis(distance)); |
void | deepSleep(long sleepFor, TimeUnit unit) sleep until timeout. if (sleepFor < 0) { throw new IllegalArgumentException("sleepFor can't be minus."); long startTimeInNanos = System.nanoTime(); long leftNanos = unit.toNanos(sleepFor); boolean isInterrupted = false; while (leftNanos > 0) { try { ... |
void | delayQuietly(final long time, final TimeUnit unit) Attempts to sleep for the specified time without throwing InterruptedException. try { Thread.sleep(unit.toMillis(time)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); |
long | diff(Date earlier, Date later, TimeUnit timeUnit) Returns the difference between two dates in the given time unit. long diffInMillies = later.getTime() - earlier.getTime(); return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); |
long | differenceBetween(Date initDate, Date endDate, TimeUnit units) difference Between return differenceBetween(initDate, endDate, units, true);
|
long | getBase(final TimeUnit unit) get Base return NANOSECONDS.convert(1, unit);
|
long | getBucketInMillis(int bucketSize, TimeUnit bucketUnit) Returns the given bucket size and bucket unit to the bucket size in milliseconds. return bucketUnit.toMillis(bucketSize);
|
Date | getComingTime(Integer delta, TimeUnit unit) get Coming Time Calendar cal = Calendar.getInstance(); long base = cal.getTimeInMillis(); long d = unit.toMillis(delta); cal.setTimeInMillis(base + d); return cal.getTime(); |
Date | getDate(final TimeUnit unit, final int offset) get Date final Calendar when = Calendar.getInstance(); long seconds = unit.toSeconds(offset); if (seconds > Integer.MAX_VALUE) { seconds = Integer.MAX_VALUE; } else if (seconds < Integer.MIN_VALUE) { seconds = Integer.MIN_VALUE; when.add(Calendar.SECOND, (int) seconds); ... |