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

longparseRelativeTimeInSeconds(String relativeTime)
parse Relative Time In Seconds
if (relativeTime.isEmpty()) {
    throw new IllegalArgumentException("exipiry time cannot be empty");
int lastIndex = relativeTime.length() - 1;
char lastChar = relativeTime.charAt(lastIndex);
final char timeUnit;
if (!Character.isAlphabetic(lastChar)) {
    timeUnit = 's';
...
DateparseSecondsFromEpoch(Double d)
Parses a double representing seconds from the epoch to a Date object
synchronized (MONITOR) {
    return new Date(SECONDS.toMillis(d.longValue()));
LongparseStringDate(String date)
parse String Date
long seconds = Long.parseLong(date);
return TimeUnit.MILLISECONDS.convert(seconds, TimeUnit.SECONDS);
longparseTimeMillis(String timeWithOrWithoutUnit)
parse Time Millis
int unitIndex = -1;
for (int i = 0; i < timeWithOrWithoutUnit.length(); i++) {
    char ch = timeWithOrWithoutUnit.charAt(i);
    if (!Character.isDigit(ch)) {
        unitIndex = i;
        break;
if (unitIndex == -1) {
    return DEFAULT_TIME_UNIT.toMillis(Integer.parseInt(timeWithOrWithoutUnit));
} else {
    int amount = Integer.parseInt(timeWithOrWithoutUnit.substring(0, unitIndex));
    String unit = timeWithOrWithoutUnit.substring(unitIndex).toLowerCase();
    TimeUnit timeUnit = null;
    int multiplyFactor = 1;
    if (unit.equals("ms")) {
        timeUnit = TimeUnit.MILLISECONDS;
    } else if (unit.equals("s")) {
        timeUnit = TimeUnit.SECONDS;
    } else if (unit.equals("m")) {
        timeUnit = TimeUnit.SECONDS;
        multiplyFactor = 60;
    } else {
        throw new RuntimeException("Unrecognized unit " + unit);
    return timeUnit.toMillis(amount * multiplyFactor);
DateparseUntilTime(byte[] untilTime)
parse Until Time
if (untilTime.length < 3)
    return null;
int year = 2000 + (untilTime[1] & 0x3f);
int month = (((untilTime[0] & 0xE0) >> 4) | ((untilTime[1] & 0xFF) >> 7)) - 1;
int day = untilTime[0] & 0x1f;
int timeInMinutes = (untilTime[2] & 0x3f) * 30;
int hours = (int) TimeUnit.MINUTES.toHours(timeInMinutes);
int minutes = (int) TimeUnit.MINUTES.toMinutes(timeInMinutes) - (int) TimeUnit.HOURS.toMinutes(hours);
...
voidpause()
pause
try {
    TimeUnit.DAYS.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
StringprettyMillis(long mil)
Convert millisecond value to human readable string hh:mm:ss.xxx
long h, m, s;
long millis = mil;
h = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(h);
m = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(m);
s = TimeUnit.MILLISECONDS.toSeconds(millis);
millis -= TimeUnit.SECONDS.toMillis(s);
...
voidprintTimeMap(Map timeMap)
print Time Map
System.out.println(toStringTimeMap(timeMap));
StringprintTimeString(long time)
print Time String
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
long diffSec = timeUnit.toSeconds(time);
long diffMin = timeUnit.toMinutes(time);
StringBuilder sb = new StringBuilder();
sb.append(time).append(" ms");
if (diffMin >= 1) {
    sb.append("(").append(diffMin).append("m").append(" ")
            .append(diffSec - TimeUnit.MINUTES.toSeconds(diffMin)).append("s").append(")");
...
StringprintTiming(long start, long end)
Print time difference in minutes, seconds and milliseconds
long totalMillis = end - start;
long mins = TimeUnit.MILLISECONDS.toMinutes(totalMillis);
long secs = TimeUnit.MILLISECONDS.toSeconds(totalMillis) - TimeUnit.MINUTES.toSeconds(mins);
long millis = TimeUnit.MILLISECONDS.toMillis(totalMillis) - TimeUnit.MINUTES.toMillis(mins)
        - TimeUnit.SECONDS.toMillis(secs);
return String.format("%d min, %d sec, %d millis", mins, secs, millis);