Java Utililty Methods TimeUnit Usage

List of utility methods to do TimeUnit Usage

Description

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

Method

longgetTomorrowTimestamp()
get Tomorrow Timestamp
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return TimeUnit.SECONDS.convert(cal.getTimeInMillis(), TimeUnit.MILLISECONDS);
TimeUnitgetUnit(long nanos)
get Unit
for (TimeUnit unit : TimeUnit.values()) {
    long current = unit.convert(nanos, TimeUnit.NANOSECONDS);
    if (current < 1000) {
        return unit;
return TimeUnit.NANOSECONDS;
StringgetValidityAsString(Date endDate)
get Validity As String
final long timeDiff = getTimeLeft(endDate);
return getFormattedTime(timeDiff);
TgetWithTimeout(final Future future)
Calls Future#get(long,TimeUnit) with TestUtils#TIMEOUT in seconds.
return future.get(TIMEOUT, TimeUnit.SECONDS);
StringhumanizeTime(long hours, long minutes, long seconds)
humanize Time
StringBuilder builder = new StringBuilder();
if (hours == 0 && minutes != 0) {
    addMinutes(builder, minutes);
} else if (hours != 0) {
    addHours(builder, hours);
    addMinutes(builder, minutes);
addSeconds(builder, seconds);
...
StringhumanizeToTime(long millis)
humanize To Time
if (millis < 1000)
    return millis + " ms";
if (millis < 1000 * 60)
    return TimeUnit.MILLISECONDS.toSeconds(millis) + " sec (" + millis + " ms)";
else
    return String.format("%02d min, %02d sec (%d ms)", TimeUnit.MILLISECONDS.toMinutes(millis),
            TimeUnit.MILLISECONDS.toSeconds(millis)
                    - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)),
...
StringhumanReadableDuration(Date from, Date to)
Prints time duration in pretty form.
StringBuilder sb = new StringBuilder();
if (from == null) {
    return "";
if (to == null) {
    return "";
long millis = to.getTime() - from.getTime();
...
StringhumanReadableMillis(long millis)
Get a human readable time milliseconds
return String.format("%d hours %d minutes, %d seconds", TimeUnit.MILLISECONDS.toHours(millis),
        TimeUnit.MILLISECONDS.toMinutes(millis)
                - TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
        TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
booleanisCurrentDateBeforeAndWithinRangeOfDate(Date date, int rangeInDays)
Returns whether or not the current date is before and within a given range in days of the passed in date.
long difference = date.getTime() - new Date().getTime();
return difference > 0 && difference < TimeUnit.DAYS.toMillis(rangeInDays);
booleanisTheSameDayCheckSummerTime(final Date day1, final Date day2, final boolean escapeYear)
is The Same Day Check Summer Time
final Calendar c = Calendar.getInstance();
c.setTime(day1);
final int y = c.get(Calendar.YEAR);
final int m = c.get(Calendar.MONTH);
final int d = c.get(Calendar.DATE);
c.setTime(day2);
return (escapeYear || y == c.get(Calendar.YEAR)) && m == c.get(Calendar.MONTH) && d == c.get(Calendar.DATE);