@Test public void testFormattingBrief() throws ParseException { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Map<String, String> datesToTest = new HashMap<String, String>(); datesToTest.put("2010-05-11 16:00:29", "Less than one minute ago"); datesToTest.put("2010-05-11 15:59:31", "Less than one minute ago"); datesToTest.put("2010-05-11 15:59:30", "1 minute ago"); datesToTest.put("2010-05-11 15:58:31", "1 minute ago"); datesToTest.put("2010-05-11 15:58:00", "2 minutes ago"); datesToTest.put("2010-05-11 15:00:30", "1 hour ago"); datesToTest.put("2010-05-11 14:00:31", "1 hour ago"); datesToTest.put("2010-05-11 14:00:30", "2 hours ago"); datesToTest.put("2010-05-11 00:00:00", "16 hours ago"); datesToTest.put("2010-05-10 16:00:30", "Yesterday"); datesToTest.put("2010-05-09 16:00:30", "Sunday"); datesToTest.put("2010-05-08 13:00:00", "Saturday"); datesToTest.put("2010-05-07 11:10:00", "Friday"); datesToTest.put("2010-05-04 16:00:30", "May 4"); datesToTest.put("2010-01-01 00:00:00", "Jan 1"); datesToTest.put("2009-12-31 23:59:59", "Dec 31, 2009"); datesToTest.put("2009-01-01 00:00:00", "Jan 1, 2009"); datesToTest.put("2008-12-31 23:59:59", "Dec 31, 2008"); Date baseDate = df.parse("2010-05-11 16:00:30"); DateFormatter sut = new DateFormatter(baseDate); for (Map.Entry<String, String> testPair : datesToTest.entrySet()) { String testDateString = testPair.getKey(); String result = sut.timeAgo(df.parse(testDateString), true); assertEquals("Incorrect format for " + testDateString, testPair.getValue(), 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);  }