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

voidsleepNanos(long nanoDuration)
sleep Nanos
final long end = System.nanoTime() + nanoDuration;
long timeLeft = nanoDuration;
do {
    if (timeLeft > SLEEP_PRECISION)
        Thread.sleep(1);
    else
        Thread.yield();
    timeLeft = end - System.nanoTime();
...
voidsleepNanos(long nanoDuration)
sleep Nanos
final long end = System.nanoTime() + nanoDuration;
long timeLeft = nanoDuration;
do {
    try {
        if (timeLeft > SLEEP_PRECISION) {
            Thread.sleep(1);
        } else if (timeLeft > SPIN_YIELD_PRECISION) {
            Thread.sleep(0);
...
voidsleepQuietly(int seconds)
Waits given time.
sleepQuietly(seconds, TimeUnit.SECONDS);
voidsleepSeconds(int secs)
Sleep for the specific number of seconds
sleepMillis(TimeUnit.SECONDS.toMillis(secs));
longstring2Millis(String duration)
string Millis
long durationMillis = 0;
duration = duration.toUpperCase().trim();
int days = duration.indexOf('D');
int hours = duration.indexOf('H');
int minutes = duration.indexOf('M');
int seconds = duration.indexOf('S');
int lastIndex = 0;
try {
...
longstring2Millis(String duration)
string Millis
long durationMillis = 0;
try {
    durationMillis = Long.parseLong(duration);
} catch (NumberFormatException nfe) {
    int indexOfM = duration.toUpperCase().indexOf('M');
    if (indexOfM > 0) {
        durationMillis = Long.parseLong(duration.substring(0, indexOfM));
        durationMillis = TimeUnit.MINUTES.toMillis(durationMillis);
...
doublestringToMillis(String string)
string To Millis
String[] tokens = string.split(":");
int secondsIndex = tokens.length - 1;
int minutesIndex = secondsIndex - 1;
int hoursIndex = minutesIndex - 1;
double seconds = (Double.parseDouble(tokens[secondsIndex].replace(",", "."))) * 1000d;
long minutes = minutesIndex > 0 ? TimeUnit.MINUTES.toMillis(Long.parseLong(tokens[minutesIndex])) : 0;
long hours = hoursIndex >= 0 ? TimeUnit.HOURS.toMillis(Long.parseLong(tokens[hoursIndex])) : 0;
return seconds + minutes + hours;
...
StringtimestampMillis()
timestamp Millis
long now = System.currentTimeMillis();
long secs = TimeUnit.MILLISECONDS.toSeconds(now);
long msecs = now - TimeUnit.SECONDS.toMillis(secs);
return String.format("%d.%03d", secs, msecs);
StringtimeToHMS(final long time)
Converts time in milliseconds to H:MM:SS format, where H is unbound.
final long hours = TimeUnit.MILLISECONDS.toHours(time);
final long minutes = TimeUnit.MILLISECONDS.toMinutes(time - TimeUnit.HOURS.toMillis(hours));
final long seconds = TimeUnit.MILLISECONDS
        .toSeconds(time - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes));
final StringBuilder sb = new StringBuilder();
sb.append(hours).append(":").append(String.format("%02d", minutes)).append(":")
        .append(String.format("%02d", seconds));
return sb.toString();
...
StringTimeToString(int ticks)
Time To String
int millis = ticks / 20 * 1000;
return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
        TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
        TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));