PrettyTime { public String format(Date then) { if (then == null) then = now(); Duration d = approximateDuration(then); return format(d); } 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 testRightNowVariance() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("moments from now", t.format(new Date(600))); }
@Test public void testMinutesFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("12 minutes from now", t.format(new Date(1000 * 60 * 12))); }
@Test public void testHoursFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 hours from now", t.format(new Date(1000 * 60 * 60 * 3))); }
@Test public void testDaysFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 days from now", t.format(new Date(1000 * 60 * 60 * 24 * 3))); }
@Test public void testWeeksFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 weeks from now", t.format(new Date(1000 * 60 * 60 * 24 * 7 * 3))); }
@Test public void testMonthsFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 months from now", t.format(new Date(2629743830L * 3L))); }
@Test public void testYearsFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 years from now", t.format(new Date(2629743830L * 12L * 3L))); }
@Test public void testDecadesFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 decades from now", t.format(new Date(315569259747L * 3L))); }
@Test public void testCenturiesFromNow() throws Exception { PrettyTime t = new PrettyTime(new Date(0)); Assert.assertEquals("3 centuries from now", t.format(new Date(3155692597470L * 3L))); }
@Test public void testMomentsAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(6000)); Assert.assertEquals("moments ago", t.format(new Date(0))); }
@Test public void testMinutesAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(1000 * 60 * 12)); Assert.assertEquals("12 minutes ago", t.format(new Date(0))); }
@Test public void testMinutesFromNowDefaultReference() throws Exception { PrettyTime t = new PrettyTime(); Assert.assertEquals("12 minutes from now", t.format(new Date(System.currentTimeMillis() + 1000 * 60 * 12))); }
@Test public void testHoursAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 3)); Assert.assertEquals("3 hours ago", t.format(new Date(0))); }
@Test public void testHoursAgoDefaultReference() throws Exception { PrettyTime t = new PrettyTime(); Assert.assertEquals("3 hours ago", t.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 3))); }
@Test public void testDaysAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 3)); Assert.assertEquals("3 days ago", t.format(new Date(0))); }
@Test public void testWeeksAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(1000 * 60 * 60 * 24 * 7 * 3)); Assert.assertEquals("3 weeks ago", t.format(new Date(0))); }
@Test public void testMonthsAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(2629743830L * 3L)); Assert.assertEquals("3 months ago", t.format(new Date(0))); }
@Test public void testYearsAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(2629743830L * 12L * 3L)); Assert.assertEquals("3 years ago", t.format(new Date(0))); }
@Test public void testDecadesAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(315569259747L * 3L)); Assert.assertEquals("3 decades ago", t.format(new Date(0))); }
@Test public void testCenturiesAgo() throws Exception { PrettyTime t = new PrettyTime(new Date(3155692597470L * 3L)); Assert.assertEquals("3 centuries ago", t.format(new Date(0))); }
@Test public void testWithinTwoHoursRounding() throws Exception { PrettyTime t = new PrettyTime(); Assert.assertEquals("2 hours ago", t.format(new Date(new Date().getTime() - 6543990))); }
@Test public void testCeilingInterval() throws Exception { Date then = format.parse("5/20/2009"); Date ref = format.parse("6/17/2009"); PrettyTime t = new PrettyTime(ref); Assert.assertEquals("1 month ago", t.format(then)); }
@Test public void testNullDate() throws Exception { PrettyTime t = new PrettyTime(); Date date = null; Assert.assertEquals("moments from now", t.format(date)); }
@Test public void testRightNow() throws Exception { PrettyTime t = new PrettyTime(); Assert.assertEquals("moments from now", t.format(new Date())); }