DateUtils { public static boolean isSameDay(Date a, Date b) { return a.getTime() / DAY_MS * DAY_MS == b.getTime() / DAY_MS * DAY_MS; }  static Date fromString(String string, String format); static Date fromString(String string); static String toString(Date date, String format); static String toString(Date date); static String toLabel(Date date, boolean isShortened); static String toLabel(Date date); static boolean isSameDay(Date a, Date b); static Date addTime(Date date, int field, int amount); static String toSimpleElapsedTime(Date date);  }
@Test (expected=NullPointerException.class) public void toLabel_null_first() { assertEquals(null, DateUtils.isSameDay(null, new Date())); }
@Test (expected=NullPointerException.class) public void toLabel_null_second() { assertEquals(null, DateUtils.isSameDay(null, new Date())); }
@Test public void toLabel_sameday_first_less() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 2:45:27"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); assertEquals(true, DateUtils.isSameDay(first, second)); }
@Test public void toLabel_sameday_second_less() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 2:45:27"); assertEquals(true, DateUtils.isSameDay(first, second)); }
@Test public void toLabel_differentday_first_less() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/04/10 EST 12:45:27"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); assertEquals(false, DateUtils.isSameDay(first, second)); }
@Test public void toLabel_differentday_second_less() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/04/10 EST 12:45:27"); assertEquals(false, DateUtils.isSameDay(first, second)); }
@Test public void toLabel_same_exact() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 12:45:27"); assertEquals(true, DateUtils.isSameDay(first, second)); }
@Test public void toLabel_midnight() throws ParseException { Date first = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/09 EST 24:00:00"); Date second = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/10 EST 24:00:00"); assertEquals(false, DateUtils.isSameDay(first, second)); }