Java Utililty Methods TimeUnit Calculate

List of utility methods to do TimeUnit Calculate

Description

The list of methods to do TimeUnit Calculate are organized into topic(s).

Method

LongdateDifference(Date date1, Date date2, TimeUnit timeUnit)
Difference between two dates
long diffInMillies = date2.getTime() - date1.getTime();
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
DatedateIn(long distance, TimeUnit unit)
date In
Date now = new Date();
return new Date(now.getTime() + unit.toMillis(distance));
voiddeepSleep(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 {
...
voiddelayQuietly(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();
longdiff(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);
longdifferenceBetween(Date initDate, Date endDate, TimeUnit units)
difference Between
return differenceBetween(initDate, endDate, units, true);
longgetBase(final TimeUnit unit)
get Base
return NANOSECONDS.convert(1, unit);
longgetBucketInMillis(int bucketSize, TimeUnit bucketUnit)
Returns the given bucket size and bucket unit to the bucket size in milliseconds.
return bucketUnit.toMillis(bucketSize);
DategetComingTime(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();
DategetDate(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);
...