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

longgetDateDiff(Date date1, Date date2, TimeUnit timeUnit)
Get a diff between two dates
long diffInMillies = date2.getTime() - date1.getTime();
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
longgetDateDiff(final Date d1, final Date d2, final TimeUnit timeUnit)
Calculates the difference between 2 date objects and returns the result in the requested time units.
long differenceInMilliseconds = d2.getTime() - d1.getTime();
return timeUnit.convert(differenceInMilliseconds, TimeUnit.MILLISECONDS);
longgetDifference(Calendar initDate, Calendar endDate, TimeUnit units)
get Difference
return units.convert(endDate.getTimeInMillis() - initDate.getTimeInMillis(), TimeUnit.MILLISECONDS);
LoadingCachegetExpiringMap(long time, TimeUnit unit)
get Expiring Map
return (LoadingCache<T, V>) CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(time, unit)
        .build(new CacheLoader() {
            public Object load(Object key) {
                throw new RuntimeException();
        });
longgetFragment(final Calendar calendar, final int fragment, final TimeUnit unit)
Gets a Calendar fragment for any unit.
if (calendar == null) {
    throw new IllegalArgumentException("The date must not be null");
long result = 0;
int offset = (unit == TimeUnit.DAYS) ? 0 : 1;
switch (fragment) {
case Calendar.YEAR:
    result += unit.convert(calendar.get(Calendar.DAY_OF_YEAR) - offset, TimeUnit.DAYS);
...
DategetFutureDate(long delay, TimeUnit timeUnit)
get Future Date
if (delay > 0) {
    return new Date(new Date().getTime() + TimeUnit.MILLISECONDS.convert(delay, timeUnit));
} else {
    return new Date();
longgetInterval(final Date startDate, final Date endDate, final TimeUnit timeUnit)
get Interval
long durationMills = endDate.getTime() - startDate.getTime();
return millisecondsTo(durationMills, timeUnit);
StringgetIntervalInfo(long intervalDuration, TimeUnit unit)
get Interval Info
TimeUnit[] timeUnits = TimeUnit.values();
long[] values = new long[timeUnits.length];
for (int i = 0; i < values.length; i++) {
    values[i] = (i == unit.ordinal()) ? intervalDuration : 0;
for (int i = 0; i < timeUnits.length - 1; i++) {
    long value = values[i];
    long nexUnitValue = timeUnits[i].convert(1, timeUnits[i + 1]);
...
longgetMilliseconds(int interval, TimeUnit unit)
get Milliseconds
long millisPerUnit = 1;
switch (unit) {
case DAYS:
    millisPerUnit = DateTimeConstants.MILLIS_PER_DAY;
    break;
case HOURS:
    millisPerUnit = DateTimeConstants.MILLIS_PER_HOUR;
    break;
...
longgetNowTimeUnit(TimeUnit timeUnit)
Get the current time in the specified time unit.
switch (timeUnit) {
case NANOSECONDS:
    return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
case MICROSECONDS:
    return TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
case MILLISECONDS:
    return TimeUnit.MILLISECONDS.toMillis(System.currentTimeMillis());
case SECONDS:
...