DateUtil { public static Date parseISO8601(final String date) { for (DatePattern pattern : DatePattern.values()) { final Calendar calendar = pattern.parse(date); if (calendar != null) { return calendar.getTime(); } } throw new IllegalArgumentException("Could not parse date " + date); }  static Date parseISO8601(final String date); static String formatISO8601(final Date date);  }
@Test public void parseISO8601NoDate() throws Exception { final Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 15); today.set(Calendar.MINUTE, 30); today.set(Calendar.SECOND, 0); today.set(Calendar.MILLISECOND, 0); Assert.assertEquals(today.getTime(), DateUtil.parseISO8601("15:30")); today.set(Calendar.MILLISECOND, 200); Assert.assertEquals(today.getTime(), DateUtil.parseISO8601("15:30:00.200000")); }
@Test public void parseISO8601Simple() throws Exception { final Calendar cal = Calendar.getInstance(); cal.set(2006, 2 - 1, 17, 0, 0, 0); cal.set(Calendar.MILLISECOND, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17")); cal.set(Calendar.HOUR_OF_DAY, 15); cal.set(Calendar.MINUTE, 30); cal.set(Calendar.SECOND, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17 15:30")); cal.set(Calendar.ZONE_OFFSET, (int) (MILLISECONDS.convert(2, HOURS) + MILLISECONDS.convert(30, MINUTES))); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17 15:30 +0230")); }
@Test public void parseISO8601T_UTCFormat() throws Exception { final Calendar cal = Calendar.getInstance(); cal.set(2006, 2 - 1, 17, 15, 30, 0); cal.set(Calendar.MILLISECOND, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17T15:30")); cal.set(Calendar.ZONE_OFFSET, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17T15:30Z")); cal.set(Calendar.MILLISECOND, 314); cal.set(Calendar.ZONE_OFFSET, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17T15:30:00.314Z")); }
@Test public void parseISO8601T_GMTFormat() throws Exception { final Calendar cal = Calendar.getInstance(); cal.set(2006, 2 - 1, 17, 15, 30, 0); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.ZONE_OFFSET, -(int) (MILLISECONDS.convert(4, HOURS))); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17T15:30-04:00")); cal.set(Calendar.MILLISECOND, 314); cal.set(Calendar.ZONE_OFFSET, -(int) (MILLISECONDS.convert(4, HOURS))); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("2006-02-17T15:30:00.314-04:00")); }
@Test public void parseISO8601CompactTFormat() throws Exception { final Calendar cal = Calendar.getInstance(); cal.set(2006, 2 - 1, 17, 15, 30, 0); cal.set(Calendar.MILLISECOND, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("20060217T1530")); cal.set(Calendar.ZONE_OFFSET, 0); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("20060217T1530Z")); cal.set(Calendar.ZONE_OFFSET, -(int) (MILLISECONDS.convert(4, HOURS))); Assert.assertEquals(cal.getTime(), DateUtil.parseISO8601("20060217T1530-0400")); }