List of utility methods to do TimeUnit Calculate
long | getDateDiff(Date date1, Date date2, TimeUnit timeUnit) Get a diff between two dates long diffInMillies = date2.getTime() - date1.getTime(); return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); |
long | getDateDiff(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); |
long | getDifference(Calendar initDate, Calendar endDate, TimeUnit units) get Difference return units.convert(endDate.getTimeInMillis() - initDate.getTimeInMillis(), TimeUnit.MILLISECONDS);
|
LoadingCache | getExpiringMap(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(); }); |
long | getFragment(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); ... |
Date | getFutureDate(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(); |
long | getInterval(final Date startDate, final Date endDate, final TimeUnit timeUnit) get Interval long durationMills = endDate.getTime() - startDate.getTime(); return millisecondsTo(durationMills, timeUnit); |
String | getIntervalInfo(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]); ... |
long | getMilliseconds(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; ... |
long | getNowTimeUnit(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: ... |