List of utility methods to do TimeUnit Usage
void | sleepNanos(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(); ... |
void | sleepNanos(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); ... |
void | sleepQuietly(int seconds) Waits given time. sleepQuietly(seconds, TimeUnit.SECONDS); |
void | sleepSeconds(int secs) Sleep for the specific number of seconds sleepMillis(TimeUnit.SECONDS.toMillis(secs)); |
long | string2Millis(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 { ... |
long | string2Millis(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); ... |
double | stringToMillis(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; ... |
String | timestampMillis() 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); |
String | timeToHMS(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(); ... |
String | TimeToString(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)); |