PrettyTime { public List<Duration> calculatePreciseDuration(Date then) { if (then == null) then = now(); Date reference = this.reference; if (null == reference) reference = now(); List<Duration> result = new ArrayList<Duration>(); long difference = then.getTime() - reference.getTime(); Duration duration = calculateDuration(difference); result.add(duration); while (0 != duration.getDelta()) { duration = calculateDuration(duration.getDelta()); if (result.size() > 0) { Duration last = result.get(result.size() - 1); if (last.getUnit().equals(duration.getUnit())) { break; } } if (duration.getUnit().isPrecise()) result.add(duration); } return result; } PrettyTime();  PrettyTime(String overrideResourceBundle);  PrettyTime(final Date reference);  PrettyTime(final Date reference, String overrideResourceBundle);  PrettyTime(final Locale locale);  PrettyTime(final Locale locale, String overrideResourceBundle);  PrettyTime(final Date reference, final Locale locale);  PrettyTime(final Date reference, final Locale locale, String overrideResourceBundle); Duration approximateDuration(Date then); List<Duration> calculatePreciseDuration(Date then); String format(Date then); String format(Calendar then); String format(final Duration duration); String format(final List<Duration> durations); String formatUnrounded(Date then); String formatUnrounded(Calendar then); String formatUnrounded(Duration duration); String formatUnrounded(List<Duration> durations); String formatDuration(Date then); String formatDuration(Calendar then); String formatDuration(Duration duration); String formatDuration(final List<Duration> durations); String formatDurationUnrounded(Date then); String formatDurationUnrounded(Calendar then); String formatDurationUnrounded(Duration duration); String formatDurationUnrounded(final List<Duration> durations); TimeFormat getFormat(TimeUnit unit); Date getReference(); PrettyTime setReference(final Date timestamp); List<TimeUnit> getUnits(); @SuppressWarnings("unchecked") UNIT getUnit(final Class<UNIT> unitType); PrettyTime registerUnit(final TimeUnit unit, TimeFormat format); TimeFormat removeUnit(final Class<UNIT> unitType); TimeFormat removeUnit(final TimeUnit unit); Locale getLocale(); PrettyTime setLocale(Locale locale); @Override String toString(); List<TimeUnit> clearUnits();  }
@Test public void testPreciseInTheFuture() throws Exception { PrettyTime t = new PrettyTime(); List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() + 1000 * (10 * 60 + 5 * 60 * 60))); Assert.assertTrue(durations.size() >= 2); Assert.assertEquals(5, durations.get(0).getQuantity()); Assert.assertEquals(10, durations.get(1).getQuantity()); }
@Test public void testPreciseInThePast() throws Exception { PrettyTime t = new PrettyTime(); List<Duration> durations = t.calculatePreciseDuration(new Date(new Date().getTime() - 1000 * (10 * 60 + 5 * 60 * 60))); Assert.assertTrue(durations.size() >= 2); Assert.assertEquals(-5, durations.get(0).getQuantity()); Assert.assertEquals(-10, durations.get(1).getQuantity()); }
@Test public void testCalculatePreciseDuration() { PrettyTime prettyTime = new PrettyTime(); prettyTime.clearUnits(); Minute minutes = new Minute(); prettyTime.registerUnit(minutes, new ResourcesTimeFormat(minutes)); Assert.assertEquals("41 minutes ago", prettyTime.format(prettyTime.calculatePreciseDuration( new Date(new Date().getTime() - 40 * 60 * 1000 - 40 * 1000)))); }