DateUtils { public static String dateToStringWithFormat(Date dateToString, String dateFormat) { if (dateToString != null) { DateFormat simpleDateFormat = new SimpleDateFormat(dateFormat); return simpleDateFormat.format(dateToString); } else { return null; } } private  DateUtils(); static Date stringToDateWithFormat(String stringToConvert, String dateFormat); static String dateToStringWithFormat(Date dateToString, String dateFormat);  }
@Test public void testDateToStringWithFormatNullDate() throws Exception { String date = DateUtils.dateToStringWithFormat(null, DateFormatConstants.DATE_FORMAT_TIME); assertNull(date); }
@Test public void testDateToStringWithFormatOk() throws Exception { Date date = getCalendar(2013, Calendar.SEPTEMBER, 29, 18, 46, 19); String expectedDate = DateUtils.dateToStringWithFormat(date, DateFormatConstants.DATE_FORMAT_TIME); assertEquals("2013-09-29T18:46:19Z", expectedDate); }
@Test public void testDateToStringWithFormatNoTimeOk() throws Exception { Date date = getCalendar(2013, Calendar.SEPTEMBER, 29, 18, 46, 19); String expectedDate = DateUtils.dateToStringWithFormat(date, DateFormatConstants.DATE_FORMAT_NO_TIME); assertEquals("2013-09-29", expectedDate); }