ResourceDateParser { public static Date parseDateString(String activeOn) { Date d; if (StringUtils.isBlank(activeOn)) { return null; } try { d = DatatypeConverter.parseDateTime(activeOn).getTime(); } catch (IllegalArgumentException e) { throw new BadRequestException("Invalid date, must use ISO 8601 format"); } return d; } private  ResourceDateParser(); static Date getFromDate(String from, String to, String days); static Date parseDateString(String activeOn);  }
@Test public void nullParseDate() { assertEquals(null, ResourceDateParser.parseDateString(null)); assertEquals(null, ResourceDateParser.parseDateString("")); assertEquals(null, ResourceDateParser.parseDateString(" ")); }
@Test(expected = BadRequestException.class) public void parseDateError() { ResourceDateParser.parseDateString("2012/13/64"); }
@Test public void parseDate() { Date parsed = ResourceDateParser.parseDateString("2012-05-29"); Calendar cal = Calendar.getInstance(); cal.setTime(parsed); assertEquals(5, cal.get(Calendar.MONTH) + 1); assertEquals(29, cal.get(Calendar.DATE)); assertEquals(2012, cal.get(Calendar.YEAR)); }
@Test public void parseDateTime() { Date parsed = ResourceDateParser.parseDateString("1997-07-16T19:20:30-00:00"); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); cal.setTime(parsed); assertEquals(7, cal.get(Calendar.MONTH) + 1); assertEquals(16, cal.get(Calendar.DATE)); assertEquals(1997, cal.get(Calendar.YEAR)); assertEquals(7, cal.get(Calendar.HOUR)); assertEquals(20, cal.get(Calendar.MINUTE)); assertEquals(30, cal.get(Calendar.SECOND)); }