### Question:
DateUtil { public static Date parse(String dateString, String format) { try { SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); Date date = sdf.parse(dateString); return date; } catch (Exception e) { LOGGER.error("error timestamp format,error:{}", e); return null; } } private  DateUtil(); static Date parseUTC(String dateString); static boolean validateUTCString(String utcDateString); static String formatUTC(Date date); static Date parse(String dateString, String format);  }### Answer:
@Test public void test_parse_noerror(){ String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = "2015-01-22 01:44:50"; Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 2015); c.set(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH, 22); c.set(Calendar.HOUR_OF_DAY, 01); c.set(Calendar.MINUTE, 44); c.set(Calendar.SECOND, 50); c.set(Calendar.MILLISECOND, 0); Date date = new Date(c.getTimeInMillis()); assertEquals(date.compareTo(DateUtil.parse(dateString, formatString)), 0); }
@Test public void test_parse_impossible_date_hasrror(){ String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = "2015-25-22 01:44:50"; assertEquals(null, DateUtil.parse(dateString, formatString)); }
@Test public void test_parse_emptydate_haserror() throws ParseException{ String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = ""; assertEquals(null, DateUtil.parse(dateString, formatString)); }
@Test public void test_parse_nulldate_haserror() throws ParseException{ String formatString = "yyyy-MM-dd HH:mm:ss"; String dateString = null; assertEquals(null, DateUtil.parse(dateString, formatString)); }