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

TimeUnitgetParentUnit(TimeUnit unit)
get Parent Unit
TimeUnit result = null;
switch (unit) {
case HOURS:
    result = TimeUnit.DAYS;
    break;
case MINUTES:
    result = TimeUnit.HOURS;
    break;
...
StringgetProperUnitName(TimeUnit unit, long amount)
Returns the appropriate spelling for a TimeUnit value based on the value of the "amount" parameter.
String proper = unit.toString().substring(0, 1)
        + unit.toString().substring(1, unit.toString().length()).toLowerCase();
return amount != 1 ? proper : proper.substring(0, proper.length() - 1);
DategetRandomTimeRound(int _duration, TimeUnit _unit)
get random time in druation whit time unit from now.
if (_duration < 0) {
    return new Date();
int i = new Random().nextInt(_duration);
return new Date(new Date().getTime() + TimeUnit.MILLISECONDS.convert(i, _unit));
intgetTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)
Floor long time to supplied Window
int ts = timeToSeconds(unit, timestamp);
return getWindowFlooredNaturalTime(ts, bucketSizeInSeconds);
longgetTimeInMillis(TimeUnit unit)
get Time In Millis
switch (unit) {
case MILLISECONDS:
case SECONDS:
case MINUTES:
case HOURS:
case DAYS:
    return unit.toMillis(1L);
default:
...
StringgetTimeString(long value, TimeUnit unit)
get Time String
if (value <= 0)
    return "";
int i = unit.ordinal();
TimeUnit[] units = TimeUnit.values();
TimeUnit next = null;
int factor = -1;
if (i < factors.length - 1) {
    next = units[i + 1];
...
TimeUnitgetTimeUnit(Object units)
get Time Unit
if (units instanceof TimeUnit) {
    return (TimeUnit) units;
} else if (units instanceof String) {
    return cleanUnitsString((String) units);
throw new IllegalArgumentException("unable to parse returned time units: " + units.toString());
TimeUnitgetTimeUnit(String timeUnit, TimeUnit defaultUnit)
get Time Unit
TimeUnit timeUnit_ = TimeUnit.SECONDS;
try {
    timeUnit_ = TimeUnit.valueOf(timeUnit);
} catch (Exception e) {
    timeUnit_ = defaultUnit;
return timeUnit_;
TimeUnitgetTimeUnitByName(String timeUnit, TimeUnit defaultTimeUnit)
get Time Unit By Name
TimeUnit timeUnit_ = TimeUnit.SECONDS;
if (timeUnit.equals("TimeUnit.SECONDS")) {
    timeUnit_ = TimeUnit.SECONDS;
} else if (timeUnit.equals("TimeUnit.DAYS")) {
    try {
        timeUnit_ = TimeUnit.valueOf("DAYS");
    } catch (Exception e) {
        timeUnit_ = defaultTimeUnit;
...
StringgetValueString(long value, TimeUnit unit)
get Value String
String result = value + " " + unit.toString().toLowerCase();
if (value == 1) {
    result = result.substring(0, result.length() - 1);
return result;