### Question:
DateUtils { public static Date parseISO8601Date(String dateString) { try { return parse(ISO8601_DATE_PATTERN, dateString); } catch (final IllegalArgumentException e) { return parse(ALTERNATE_ISO8601_DATE_PATTERN, dateString); } }  static Date parse(String pattern, String dateString); static String format(String pattern, Date date); static Date parseISO8601Date(String dateString); static String formatISO8601Date(Date date); static Date parseRFC822Date(String dateString); static String formatRFC822Date(Date date); static Date parseCompressedISO8601Date(String dateString); static Date cloneDate(Date date); static long numberOfDaysSinceEpoch(long milliSinceEpoch); static final String ISO8601_DATE_PATTERN; static final String ALTERNATE_ISO8601_DATE_PATTERN; static final String RFC822_DATE_PATTERN; static final String COMPRESSED_DATE_PATTERN; }### Answer:
@Test public void testAlternativeIso8601Date() throws ParseException { Date date = new Date(); String formatted = alternateIso8601DateFormat.print(date.getTime()); Date expectedDate = new Date(alternateIso8601DateFormat.parseMillis(formatted)); Date actualDate = DateUtils.parseISO8601Date(formatted); assertEquals(expectedDate, actualDate); }
@Test(expected = IllegalArgumentException.class) public void invalidDate() throws ParseException { final String input = "2014-03-06T14:28:58.000 Z"; System.out.println(DateUtils.parseISO8601Date(input)); }
@Test public void testIssueDaysDiff() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); sdf.setTimeZone(new SimpleTimeZone(0, "GMT")); String edgeCase = "292278994-08-17T07:12:55.807Z"; String testCase = "292278993-08-17T07:12:55.807Z"; Date od = DateUtils.parseISO8601Date(edgeCase); Date testd = DateUtils.parseISO8601Date(testCase); long diff = od.getTime() - testd.getTime(); assertTrue(diff == 365L * 24 * 60 * 60 * 1000); }
@Test public void testIssue233Overflows() throws ParseException { String[] cases = { "292278994-08-17T07:12:55.808Z", "292278995-01-17T07:12:55.807Z", }; for (String edgeCase : cases) { try { Date parsed = DateUtils.parseISO8601Date(edgeCase); System.out.println("date: " + parsed); fail("Unexpected success: " + parsed); } catch (IllegalArgumentException ex) { assertTrue(ex.getCause() instanceof ParseException); } } }