ISO8601Util { public Calendar toCalendar(final String iso8601String) throws ParseException { Pattern pattern = Pattern.compile(REGEX_ISO8601); Matcher matcher = pattern.matcher(iso8601String); if (matcher.matches()) { int year = matcher.group(1) != null ? Integer.valueOf(matcher.group(1)) : 0; int month = matcher.group(2) != null ? Integer.valueOf(matcher.group(2)) - 1 : 0; int day = matcher.group(3) != null ? Integer.valueOf(matcher.group(3)) : 0; int h = matcher.group(4) != null ? Integer.valueOf(matcher.group(4)) : 0; int m = matcher.group(5) != null ? Integer.valueOf(matcher.group(5)) : 0; int s = matcher.group(6) != null ? Integer.valueOf(matcher.group(6)) : 0; int ms = Math.round(Float.parseFloat("0." + (matcher.group(7) != null ? matcher.group(7) : "0")) * 1000); TimeZone timeZone = TimeZone.getTimeZone("GMT" + (matcher.group(8) != null ? matcher.group(8) : "")); Calendar calendar = GregorianCalendar.getInstance(timeZone); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, h); calendar.set(Calendar.MINUTE, m); calendar.set(Calendar.SECOND, s); calendar.set(Calendar.MILLISECOND, ms); return calendar; } else throw new java.text.ParseException("Unparseable ISO8601 date format: " + iso8601String, 0); }  String fromCalendar(final Calendar calendar); String fromDate(final Date date); String now(); Calendar toCalendar(final String iso8601String); Date toDate(final String iso8601String);  }
@Test public void shouldParseDateExtendFormat() throws ParseException { Calendar parseIso8601 = iso8601.toCalendar("1982-06-10"); Calendar date = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); date.set(1982, 5, 10, 0, 0, 0); date.set(Calendar.MILLISECOND, 0); assertThat(parseIso8601, is(date)); }
@Test public void shouldParseDateBasicFormat() throws ParseException { Calendar parseIso8601 = iso8601.toCalendar("19820610"); Calendar date = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); date.set(1982, 5, 10, 0, 0, 0); date.set(Calendar.MILLISECOND, 0); assertThat(parseIso8601, is(date)); }
@Test public void shouldParseDateTimeExtendFormat() throws ParseException { Calendar parseIso8601 = iso8601.toCalendar("1982-06-10T05:35:10.000-03:00"); Calendar date = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT-03:00")); date.set(1982, 5, 10, 5, 35, 10); date.set(Calendar.MILLISECOND, 0); assertThat(parseIso8601, is(date)); }
@Test public void shouldParseDateTimeBasicFormat() throws ParseException { Calendar parseIso8601 = iso8601.toCalendar("19820610T05-03"); Calendar date = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT-03:00")); date.set(1982, 5, 10, 5, 0, 0); date.set(Calendar.MILLISECOND, 0); assertThat(parseIso8601, is(date)); }
@Test public void shouldParseDateTimeExtendFormatUTC() throws ParseException { Calendar parseIso8601 = iso8601.toCalendar("1982-06-10T05:35:10Z"); Calendar date = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT")); date.set(1982, 5, 10, 5, 35, 10); date.set(Calendar.MILLISECOND, 0); assertThat(parseIso8601, is(date)); }
@Test(expected=ParseException.class) public void expectParseExceptionOnDateWithBars() throws ParseException { iso8601.toCalendar("1982/06/10"); }