DateUtils { public static String toString(Date date, String format) { return date == null ? null : FORMATTERS.get(format).print(date.getTime()); }  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 public void toString_format_builtinformat() throws ParseException { Date input = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2011/05/12 12:45:27"); assertEquals("2011-05-12 12:45:27", DateUtils.toString(input, "yyyy-MM-dd HH:mm:ss")); }
@Test public void toString_format_newformat() throws ParseException { Date input = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2011/05/12 12:45:27"); assertEquals("2011/05/12 12:45:27", DateUtils.toString(input, "yyyy/MM/dd HH:mm:ss")); }
@Test public void toString_format_nulldate() throws ParseException { assertEquals(null, DateUtils.toString(null, "yyyy-MM-dd HH:mm:ss")); }
@Test (expected=NullPointerException.class) public void toString_format_nullformat() throws ParseException { Date input = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2011/05/12 12:45:27"); assertEquals(null, DateUtils.toString(input, null)); }
@Test public void toString_noformat_builtinformat() throws ParseException { Date input = new SimpleDateFormat("yyyy/MM/dd z HH:mm:ss").parse("2011/05/12 EST 12:45:27"); assertEquals("2011-05-12T17:45:27Z", DateUtils.toString(input)); }
@Test public void toString_noformat_nulldate() throws ParseException { assertEquals(null, DateUtils.toString(null)); }