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

Stringabbreviate(final TimeUnit unit)
abbreviate
switch (unit) {
case NANOSECONDS:
    return "ns";
case MICROSECONDS:
    return "\u03bcs"; 
case MILLISECONDS:
    return "ms";
case SECONDS:
...
voidaddTimeUnit(Calendar cal, int unit, int val)
Add a quantity of time units to a Calendar
addTimeUnit(cal, unit, (long) val);
voidappendTimeout(StringBuilder b, long timeout, TimeUnit unit)
append Timeout
if (timeout == 0) {
    b.append('0');
} else if (timeout < 0) {
    b.append("infinite");
} else {
    b.append(timeout);
    b.append(' ');
    String unitStr = unit.toString().toLowerCase();
...
longbetween(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale)
Compare two dates and returns the duration between them following the time scale.
Objects.requireNonNull(calendar1, "The parameter calendar1 cannot be null");
Objects.requireNonNull(calendar2, "The parameter calendar2 cannot be null");
return between(calendar1.getTime(), calendar2.getTime(), scale);
longcalculateDateDiff(Date date1, Date date2, TimeUnit timeUnit)
calculate Date Diff
long diffInMillies = date2.getTime() - date1.getTime();
return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
longcalculateDelatBasedOnRate(int rateLimit, TimeUnit periodUnit, int period, TimeUnit resultingTimeUnit)
calculate Delat Based On Rate
return resultingTimeUnit.convert(period, periodUnit) / rateLimit;
longcalculateTimeout(long timeout, TimeUnit timeUnit)
calculate Timeout
timeUnit = timeUnit == null ? TimeUnit.SECONDS : timeUnit;
return timeUnit.toNanos(timeout);
voidcheckTime(long time, TimeUnit timeunit)
check Time
if (time < 0) {
    throw new IllegalArgumentException("Time can not be less than 0.");
if (timeunit == null) {
    throw new NullPointerException("TimeUnit can not be null.");
voidcheckTimeUnit(TimeUnit timeUnit)
check Time Unit
if (timeUnit == TimeUnit.NANOSECONDS || timeUnit == TimeUnit.MICROSECONDS) {
    throw new IllegalArgumentException("Milliseconds is the lowest permitted time unit!");
Calendar[]createEquidistantDates(Calendar reference, final int periods, final TimeUnit sampleUnit, final int sampleRate, Calendar calendar)
create Equidistant Dates
if (reference == null) {
    reference = Calendar.getInstance();
assert (periods < 0) : "Number of periods must be " + "nonnegative.\n" + "periods: " + periods;
assert (sampleRate == 0) : "Sample rate must not be 0.";
if (calendar == null) {
    calendar = Calendar.getInstance();
Calendar[] dates = new Calendar[periods + 1];
calendar.setLenient(true);
int field = 0;
dates[0] = reference;
for (int i = 1; i <= periods; i++) {
    calendar.add(field, sampleRate);
    dates[i] = calendar;
return dates;