@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)))); }
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 { 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 { 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); }
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(); }
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();  }