@Test public void testConstructor() { Date date = new Date(); String result = new DateFormatter().timeAgo(date); assertEquals("Default constructor should initialize with current time.", "Less than one minute ago", result); }
@SuppressWarnings("deprecation") public static String timeAgo(final Date theDate, final Date nowDate, final boolean brief) { StringBuffer sb = new StringBuffer(""); long deltaMilliseconds = nowDate.getTime() - theDate.getTime(); if (deltaMilliseconds < 2 * MILLISECONDS_PER_DAY) { int dayOfWeekDelta = nowDate.getDay() - theDate.getDay(); if (dayOfWeekDelta == 1 || dayOfWeekDelta == 1 - 7) { if (brief) { return "Yesterday"; } else { sb.append("Yesterday at "); appendTimeOfDay(sb, theDate); return sb.toString(); } } } if (deltaMilliseconds < MILLISECONDS_PER_MINUTE) { sb.append("Less than one minute ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_MINUTE) { sb.append("1 minute ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_HOUR) { sb.append(deltaMilliseconds / MILLISECONDS_PER_MINUTE); sb.append(" minutes ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_HOUR) { sb.append("1 hour ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_DAY) { sb.append(deltaMilliseconds / MILLISECONDS_PER_HOUR); sb.append(" hours ago"); } else { if (deltaMilliseconds < 7 * MILLISECONDS_PER_DAY && nowDate.getDay() != theDate.getDay()) { appendDayOfWeek(sb, theDate); } else { appendDate(sb, theDate, theDate.getYear() != nowDate.getYear()); } if (!brief) { sb.append(" at "); appendTimeOfDay(sb, theDate); } } return sb.toString(); }
DateFormatter { @SuppressWarnings("deprecation") public static String timeAgo(final Date theDate, final Date nowDate, final boolean brief) { StringBuffer sb = new StringBuffer(""); long deltaMilliseconds = nowDate.getTime() - theDate.getTime(); if (deltaMilliseconds < 2 * MILLISECONDS_PER_DAY) { int dayOfWeekDelta = nowDate.getDay() - theDate.getDay(); if (dayOfWeekDelta == 1 || dayOfWeekDelta == 1 - 7) { if (brief) { return "Yesterday"; } else { sb.append("Yesterday at "); appendTimeOfDay(sb, theDate); return sb.toString(); } } } if (deltaMilliseconds < MILLISECONDS_PER_MINUTE) { sb.append("Less than one minute ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_MINUTE) { sb.append("1 minute ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_HOUR) { sb.append(deltaMilliseconds / MILLISECONDS_PER_MINUTE); sb.append(" minutes ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_HOUR) { sb.append("1 hour ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_DAY) { sb.append(deltaMilliseconds / MILLISECONDS_PER_HOUR); sb.append(" hours ago"); } else { if (deltaMilliseconds < 7 * MILLISECONDS_PER_DAY && nowDate.getDay() != theDate.getDay()) { appendDayOfWeek(sb, theDate); } else { appendDate(sb, theDate, theDate.getYear() != nowDate.getYear()); } if (!brief) { sb.append(" at "); appendTimeOfDay(sb, theDate); } } return sb.toString(); } }
DateFormatter { @SuppressWarnings("deprecation") public static String timeAgo(final Date theDate, final Date nowDate, final boolean brief) { StringBuffer sb = new StringBuffer(""); long deltaMilliseconds = nowDate.getTime() - theDate.getTime(); if (deltaMilliseconds < 2 * MILLISECONDS_PER_DAY) { int dayOfWeekDelta = nowDate.getDay() - theDate.getDay(); if (dayOfWeekDelta == 1 || dayOfWeekDelta == 1 - 7) { if (brief) { return "Yesterday"; } else { sb.append("Yesterday at "); appendTimeOfDay(sb, theDate); return sb.toString(); } } } if (deltaMilliseconds < MILLISECONDS_PER_MINUTE) { sb.append("Less than one minute ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_MINUTE) { sb.append("1 minute ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_HOUR) { sb.append(deltaMilliseconds / MILLISECONDS_PER_MINUTE); sb.append(" minutes ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_HOUR) { sb.append("1 hour ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_DAY) { sb.append(deltaMilliseconds / MILLISECONDS_PER_HOUR); sb.append(" hours ago"); } else { if (deltaMilliseconds < 7 * MILLISECONDS_PER_DAY && nowDate.getDay() != theDate.getDay()) { appendDayOfWeek(sb, theDate); } else { appendDate(sb, theDate, theDate.getYear() != nowDate.getYear()); } if (!brief) { sb.append(" at "); appendTimeOfDay(sb, theDate); } } return sb.toString(); } DateFormatter();  DateFormatter(final Date inBaseDate); }
DateFormatter { @SuppressWarnings("deprecation") public static String timeAgo(final Date theDate, final Date nowDate, final boolean brief) { StringBuffer sb = new StringBuffer(""); long deltaMilliseconds = nowDate.getTime() - theDate.getTime(); if (deltaMilliseconds < 2 * MILLISECONDS_PER_DAY) { int dayOfWeekDelta = nowDate.getDay() - theDate.getDay(); if (dayOfWeekDelta == 1 || dayOfWeekDelta == 1 - 7) { if (brief) { return "Yesterday"; } else { sb.append("Yesterday at "); appendTimeOfDay(sb, theDate); return sb.toString(); } } } if (deltaMilliseconds < MILLISECONDS_PER_MINUTE) { sb.append("Less than one minute ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_MINUTE) { sb.append("1 minute ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_HOUR) { sb.append(deltaMilliseconds / MILLISECONDS_PER_MINUTE); sb.append(" minutes ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_HOUR) { sb.append("1 hour ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_DAY) { sb.append(deltaMilliseconds / MILLISECONDS_PER_HOUR); sb.append(" hours ago"); } else { if (deltaMilliseconds < 7 * MILLISECONDS_PER_DAY && nowDate.getDay() != theDate.getDay()) { appendDayOfWeek(sb, theDate); } else { appendDate(sb, theDate, theDate.getYear() != nowDate.getYear()); } if (!brief) { sb.append(" at "); appendTimeOfDay(sb, theDate); } } return sb.toString(); } DateFormatter();  DateFormatter(final Date inBaseDate); @SuppressWarnings("deprecation") static String timeAgo(final Date theDate, final Date nowDate, final boolean brief); String timeAgo(final Date theDate); String timeAgo(final Date theDate, final boolean brief); }
DateFormatter { @SuppressWarnings("deprecation") public static String timeAgo(final Date theDate, final Date nowDate, final boolean brief) { StringBuffer sb = new StringBuffer(""); long deltaMilliseconds = nowDate.getTime() - theDate.getTime(); if (deltaMilliseconds < 2 * MILLISECONDS_PER_DAY) { int dayOfWeekDelta = nowDate.getDay() - theDate.getDay(); if (dayOfWeekDelta == 1 || dayOfWeekDelta == 1 - 7) { if (brief) { return "Yesterday"; } else { sb.append("Yesterday at "); appendTimeOfDay(sb, theDate); return sb.toString(); } } } if (deltaMilliseconds < MILLISECONDS_PER_MINUTE) { sb.append("Less than one minute ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_MINUTE) { sb.append("1 minute ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_HOUR) { sb.append(deltaMilliseconds / MILLISECONDS_PER_MINUTE); sb.append(" minutes ago"); } else if (deltaMilliseconds < 2 * MILLISECONDS_PER_HOUR) { sb.append("1 hour ago"); } else if (deltaMilliseconds < MILLISECONDS_PER_DAY) { sb.append(deltaMilliseconds / MILLISECONDS_PER_HOUR); sb.append(" hours ago"); } else { if (deltaMilliseconds < 7 * MILLISECONDS_PER_DAY && nowDate.getDay() != theDate.getDay()) { appendDayOfWeek(sb, theDate); } else { appendDate(sb, theDate, theDate.getYear() != nowDate.getYear()); } if (!brief) { sb.append(" at "); appendTimeOfDay(sb, theDate); } } return sb.toString(); } DateFormatter();  DateFormatter(final Date inBaseDate); @SuppressWarnings("deprecation") static String timeAgo(final Date theDate, final Date nowDate, final boolean brief); String timeAgo(final Date theDate); String timeAgo(final Date theDate, final boolean brief);  }