List of utility methods to do TimeUnit Usage
long | parseRelativeTimeInSeconds(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'; ... |
Date | parseSecondsFromEpoch(Double d) Parses a double representing seconds from the epoch to a Date object synchronized (MONITOR) { return new Date(SECONDS.toMillis(d.longValue())); |
Long | parseStringDate(String date) parse String Date long seconds = Long.parseLong(date); return TimeUnit.MILLISECONDS.convert(seconds, TimeUnit.SECONDS); |
long | parseTimeMillis(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); |
Date | parseUntilTime(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); ... |
void | pause() pause try { TimeUnit.DAYS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); |
String | prettyMillis(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); ... |
void | printTimeMap(Map print Time Map System.out.println(toStringTimeMap(timeMap)); |
String | printTimeString(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(")"); ... |
String | printTiming(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); |