Java Utililty Methods TimeUnit Convert

List of utility methods to do TimeUnit Convert

Description

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

Method

intdurationToMillis(final long val, final TimeUnit unit)
Converts the given duration (interval value plus unit) to milliseconds, ensuring that any given value greater than zero converts to at least one millisecond to avoid a zero millisecond result, since Object.wait(0) waits forever.
if (val == 0) {
    return 0;
if (unit == null) {
    throw new IllegalArgumentException(
            "Duration TimeUnit argument may not be null if interval " + "is non-zero");
if (val < 0) {
...
DategetDateRelativeToNow(TimeUnit timeUnit, long amount)
Builds and returns a date that is a specified amount of time away from now (earlier or later).
return new Date(System.currentTimeMillis() + timeUnit.toMillis(amount));
longgetRemainingTimeToday(final TimeUnit timeUnit)
get Remaining Time Today
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
long durationMills = todayEnd.getTimeInMillis() - System.currentTimeMillis();
return millisecondsTo(durationMills, timeUnit);
doublehoursToUnit(double hours, TimeUnit destinationUnit)
hours To Unit
switch (destinationUnit) {
case NANOSECONDS:
    return hours * (C5 / C0);
case MICROSECONDS:
    return hours * (C5 / C1);
case MILLISECONDS:
    return hours * (C5 / C2);
case SECONDS:
...
longmillisecondsTo(long milliseconds, final TimeUnit timeUnit)
milliseconds To
switch (timeUnit) {
case MILLISECONDS:
    return TimeUnit.MILLISECONDS.toMillis(milliseconds);
case NANOSECONDS:
    return TimeUnit.MILLISECONDS.toNanos(milliseconds);
case MICROSECONDS:
    return TimeUnit.MILLISECONDS.toMicros(milliseconds);
case SECONDS:
...
longmillisToDuration(final int val, final TimeUnit unit)
Converts the given duration value in milliseconds to the given unit.
if (unit == null) {
    throw new IllegalArgumentException("TimeUnit argument may not be null");
return unit.convert(val, TimeUnit.MILLISECONDS);
TimeUnitstringToTimeUnit(String str)
string To Time Unit
switch (str.toLowerCase()) {
case "ms":
case "millisecond":
case "milliseconds":
    return TimeUnit.MILLISECONDS;
case "s":
case "sec":
case "second":
...
Datesubstract(Date date, long durationToSubstract, TimeUnit unit)
substract
long result = unit.toSeconds(durationToSubstract);
if (result >= Integer.MAX_VALUE) {
    throw new IllegalArgumentException("Cannot substract " + durationToSubstract);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.SECOND, -(int) result);
return c.getTime();
...
longtimeToNanoSeconds(TimeUnit unit, long time)
time To Nano Seconds
long ts;
switch (unit) {
case NANOSECONDS:
    ts = time;
    break;
case MICROSECONDS:
    ts = ((long) time * (1000));
    break;
...
IntervaltoIntervalFromNow(long timeback, TimeUnit unit)
to Interval From Now
Date now = new Date();
return toInterval(substract(now, timeback, unit), now);