List of utility methods to do TimeUnit Usage
String | getDurationBreakdown(long millis) Convert a millisecond duration to a string format long hours = TimeUnit.MILLISECONDS.toHours(millis); millis -= TimeUnit.HOURS.toMillis(hours); long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); millis -= TimeUnit.MINUTES.toMillis(minutes); long seconds = TimeUnit.MILLISECONDS.toSeconds(millis); return String.format("%d:%02d:%02d", hours, minutes, seconds); |
String | getDurationFromMillis(long millis) get Duration From Millis return String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); |
String | getDurationFromMilliseconds(long duration) Gets the duration as string. long days = TimeUnit.MILLISECONDS.toDays(duration); duration = duration - TimeUnit.DAYS.toMillis(days); long hours = TimeUnit.MILLISECONDS.toHours(duration); duration = duration - TimeUnit.HOURS.toMillis(hours); long minutes = TimeUnit.MILLISECONDS.toMinutes(duration); duration = duration - TimeUnit.MINUTES.toMillis(minutes); long seconds = TimeUnit.MILLISECONDS.toSeconds(duration); duration = duration - TimeUnit.SECONDS.toMillis(seconds); ... |
String | getDurationFromTwoDates(Date startTime, Date endTime) get Duration From Two Dates long duration = endTime.getTime() - startTime.getTime(); return getDurationFromMillis(duration); |
String | getDurationHMS(long millis) get Duration HMS String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)), TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))); return hms; |
TimeUnit | getDurationUnit(Map get Duration Unit String durationUnit = (String) config.getOrDefault("durationUnit", TimeUnit.SECONDS.name()); return TimeUnit.valueOf(durationUnit.toUpperCase()); |
String | getElapsedTime(Date start, Date end) get Elapsed Time long duration = end.getTime() - start.getTime(); final long days = TimeUnit.MILLISECONDS.toDays(duration); duration -= TimeUnit.DAYS.toMillis(days); final long hours = TimeUnit.MILLISECONDS.toHours(duration); duration -= TimeUnit.HOURS.toMillis(hours); final long minutes = TimeUnit.MILLISECONDS.toMinutes(duration); duration -= TimeUnit.MINUTES.toMillis(minutes); final long seconds = TimeUnit.MILLISECONDS.toSeconds(duration); ... |
String | getElapsedTimeString(long elapsed) get Elapsed Time String String elapsedStr = String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(elapsed), TimeUnit.MILLISECONDS.toSeconds(elapsed) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsed))); return elapsedStr; |
String | getETA(final long seconds) get ETA long delta = seconds; StringBuilder sb = new StringBuilder(); final long days = seconds / SECONDS_IN_DAY; if (days > 0) { sb.append(days).append("d "); delta -= days * SECONDS_IN_DAY; final long hours = delta / SECONDS_IN_HOUR; ... |
String | getFormattedDiffTime(long timeFirst, long timeLast) Format diff time from milliseconds long timeDiff = timeLast - timeFirst; long ms = timeDiff % 1000; long sec = TimeUnit.MILLISECONDS.toSeconds(timeDiff) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(timeDiff)); long min = TimeUnit.MILLISECONDS.toMinutes(timeDiff); if (min > 29) { return "Time diff > 30m"; } else if (min > 0) { ... |