ISO8601Date { public static Calendar toCalendar(final String iso8601string) throws ParseException { Calendar calendar = GregorianCalendar.getInstance(); String s = iso8601string.replaceAll("Z$", "+00:00"); int lastIndex = s.lastIndexOf(":"); s = s.substring(0, lastIndex) + s.substring(lastIndex + 1); Date date; synchronized (TO_CALENDAR_FORMATTER) { date = TO_CALENDAR_FORMATTER.parse(s); } calendar.setTime(date); return calendar; }  static String fromCalendar(final Calendar calendar); static String now(); static Calendar toCalendar(final String iso8601string);  }
@Test public void testParseString() throws ParseException { Calendar tokyoCalendar = ISO8601Date.toCalendar(TEST_TIME_TOKYO); tokyoCalendar.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")); assertEquals(Calendar.JULY, tokyoCalendar.get(Calendar.MONTH)); assertEquals(17, tokyoCalendar.get(Calendar.DAY_OF_MONTH)); assertEquals(16, tokyoCalendar.get(Calendar.HOUR_OF_DAY)); assertEquals(11, tokyoCalendar.get(Calendar.MINUTE)); assertEquals(18, tokyoCalendar.get(Calendar.SECOND)); assertEquals(640, tokyoCalendar.get(Calendar.MILLISECOND)); Calendar londonCalendar = ISO8601Date.toCalendar(TEST_TIME_LONDON); londonCalendar.setTimeZone(TimeZone.getTimeZone("UTC")); assertEquals(Calendar.JULY, londonCalendar.get(Calendar.MONTH)); assertEquals(17, londonCalendar.get(Calendar.DAY_OF_MONTH)); assertEquals(16, londonCalendar.get(Calendar.HOUR_OF_DAY)); assertEquals(11, londonCalendar.get(Calendar.MINUTE)); assertEquals(18, londonCalendar.get(Calendar.SECOND)); assertEquals(640, londonCalendar.get(Calendar.MILLISECOND)); }