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

intgetWindowFlooredBinaryTime(TimeUnit unit, long timestamp, int windowSizeInSeconds)
get Window Floored Binary Time
timestamp = timeToNanoSeconds(unit, timestamp);
System.err.println("shift:" + (MAX_BITS_TO_SHIFT - Integer.numberOfLeadingZeros(windowSizeInSeconds)));
int ts = (int) (timestamp >> (MAX_BITS_TO_SHIFT - Integer.numberOfLeadingZeros(windowSizeInSeconds)));
return ts;
StringhumanReadableTime(long time, TimeUnit unit)
Convert the given time to a human readable string representation.
final TimeUnit unit1 = largestNonzeroUnit(time, unit);
final long value1 = time / unit.convert(1, unit1);
final long timeRemainder = time % unit.convert(1, unit1);
if (unit1 == NANOSECONDS || timeRemainder == 0) {
    return String.format("%d %s", value1, unit1.name().toLowerCase());
} else {
    final TimeUnit unit2 = largestNonzeroUnit(timeRemainder, unit);
    final long value2 = timeRemainder / unit.convert(1, unit2);
...
booleanisValidTimeUnit(String timeUnit)
is Valid Time Unit
for (TimeUnit type : TimeUnit.values()) {
    if (type.name().equalsIgnoreCase(timeUnit)) {
        return true;
return false;
voidlogTiming(String description, int n, long time, TimeUnit unit)
log Timing
System.out.format("%s = %,d %s (n = %,d)%n", description, time, unit.name().toLowerCase(), n);
voidmapByName(TimeUnit... units)
map By Name
for (TimeUnit unit : units) {
    UNIT_MAPPING.put(unit.name().toLowerCase(), unit);
Dateminus(Date date, int interval, TimeUnit unit)
minus
switch (unit) {
case DAYS:
    return minusDays(date, interval);
case HOURS:
    return minusHours(date, interval);
case MINUTES:
    return minusMinutes(date, interval);
case SECONDS:
...
OptionalparseTimeUnit(final String unit)
parse Time Unit
return Optional.ofNullable(unit).map(String::toUpperCase).map(TimeUnit::valueOf);
TimeUnitparseTimeUnit(Object property, TimeUnit defaultValue)
parse Time Unit
try {
    if (property instanceof String)
        return TimeUnit.valueOf((String) property);
    if (property instanceof TimeUnit)
        return (TimeUnit) property;
    if (property != null)
        return TimeUnit.valueOf(property.toString());
} catch (Exception ignore) {
...
voidpause(long duration, TimeUnit unit)
pause
try {
    Thread.sleep(unit.toMillis(duration));
} catch (InterruptedException e) {
StringprintTime(long time, TimeUnit unit)
print Time
long ns = TimeUnit.NANOSECONDS.convert(time, unit);
long us = TimeUnit.MICROSECONDS.convert(time, unit);
long ms = TimeUnit.MILLISECONDS.convert(time, unit);
long secs = TimeUnit.SECONDS.convert(time, unit);
if (secs > 0)
    return secs + "s";
if (ms > 0)
    return ms + "ms";
...