DateTimeUtil { public static String formatDuration(final long duration, final boolean longFormat, final boolean trimZeros) { if (duration < 0) throw new IllegalArgumentException("Duration must be greater than or equal to zero."); if (duration == 0L) return longFormat ? "0 miliseconds" : "0ms"; long tm = duration; int days = (int) (tm / 86400000); tm = tm - (days * 86400000L); int hours = (int) (tm / 3600000); tm = tm - (hours * 3600000L); int mins = (int) (tm / 60000); tm = tm - (mins * 60000L); int secs = (int) (tm / 1000); tm = tm - (secs * 1000L); int msecs = (int) tm; StringBuilder ret = new StringBuilder(); if (days > 0 || !trimZeros) ret.append(days).append(longFormat ? " days " : "d "); if (hours > 0 || !trimZeros) ret.append(hours).append(longFormat ? " hours " : "h "); if (mins > 0 || !trimZeros) ret.append(mins).append(longFormat ? " minutes " : "m "); if (secs > 0 || !trimZeros) ret.append(secs).append(longFormat ? " seconds " : "s "); if (msecs > 0 || !trimZeros) ret.append(msecs).append(longFormat ? " miliseconds" : "ms"); return ret.toString(); } private  DateTimeUtil(); static String formatDuration(final long duration, final boolean longFormat, final boolean trimZeros); static Date now(); static Date nowZeroSec(); static Calendar nowCalendar(); static Date today(); static Calendar todayCalendar(); static Date yesterday(); static Calendar yesterdayCalendar(); static Date addTime(final Date date, final int period, final int amount); static Calendar addTimeCalendar(final Calendar calendar, final int period, final int amount); static Calendar date2calendar(final Date date); static Calendar parseDate(final String date, final boolean withTime); static Calendar parseDate(final String date, final String pattern); static Calendar parseISODateTime(final String dateTime); static Calendar parseDefaultISODateTime(final String dateTime); static String formatDate(final Date date, final boolean withTime); static String formatDate(final Date date, final String pattern); static String formatDateByLocale(final Date date, Locale locale, int style); static String formatDateTimeByLocale(final Date date, Locale locale, int style); static int extractYear(final Date date); static int getFirstDayOfWeek(Locale locale); static boolean inPast(Date time); static Date getMidnight(Date time); static final String PATTERN_CZ_DATE; static final String PATTERN_CZ_DATETIME; static final String PATTERN_CZ_TIME; static final String PATTERN_LOG; }
@Test public void testFormatDuration() { try { DateTimeUtil.formatDuration(-1, true, true); fail(); } catch (IllegalArgumentException expected) { } assertEquals("0 miliseconds", DateTimeUtil.formatDuration(0, true, true)); assertEquals("0 miliseconds", DateTimeUtil.formatDuration(0, true, false)); assertEquals("0ms", DateTimeUtil.formatDuration(0, false, true)); assertEquals("0ms", DateTimeUtil.formatDuration(0, false, false)); assertEquals("0 days 10 hours 0 minutes 47 seconds 972 miliseconds", DateTimeUtil.formatDuration(36047972, true, false)); assertEquals("10 hours 47 seconds 972 miliseconds", DateTimeUtil.formatDuration(36047972, true, true)); assertEquals("0d 10h 0m 47s 972ms", DateTimeUtil.formatDuration(36047972, false, false)); assertEquals("10h 47s 972ms", DateTimeUtil.formatDuration(36047972, false, true)); }