DateParser { public static Calendar parse(String str) throws ParseException { if (str == null) { return null; } Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); cal.clear(); int len = str.length(); if (len == 0) { return cal; } int i = 0; i = readYear(cal, str, i); i = readCharOpt('-', cal, str, i); if (i == -1) { return cal; } i = readMonth(cal, str, i); i = readCharOpt('-', cal, str, i); if (i == -1) { return cal; } i = readDay(cal, str, i); i = readCharOpt('T', cal, str, i); if (i == -1) { return cal; } i = readHours(cal, str, i); i = readCharOpt(':', cal, str, i); if (i == -1) { return cal; } i = readMinutes(cal, str, i); if (isChar(':', str, i)) { i = readSeconds(cal, str, i + 1); if (isChar('.', str, i)) { i = readMilliseconds(cal, str, i + 1); } } if (i > -1) { readTimeZone(cal, str, i); } return cal; }  static Calendar parse(String str); static Date parseW3CDateTime(String str); static String formatW3CDateTime(Date date);  }
@Test public void parseTestDate() throws Exception { Calendar cal = DateParser.parse("2012-11-29T13:15:18.176+0000"); assertEquals(13,cal.get(Calendar.HOUR_OF_DAY)); cal = DateParser.parse("2012-11-29T13:15:18.176+0200"); assertEquals(11,cal.get(Calendar.HOUR_OF_DAY)); }
@Test public void parseTimeZone() throws Exception { Calendar cal = DateParser.parse("2012-11-29T13:15:18.176Z"); assertEquals(13,cal.get(Calendar.HOUR_OF_DAY)); cal = DateParser.parse("2012-11-29T13:15:18.176+0100"); assertEquals(12,cal.get(Calendar.HOUR_OF_DAY)); cal = DateParser.parse("2012-11-29T13:15:18.176+01:00"); assertEquals(12,cal.get(Calendar.HOUR_OF_DAY)); cal = DateParser.parse("2012-11-29T13:15:18.176+01"); assertEquals(12,cal.get(Calendar.HOUR_OF_DAY)); cal = DateParser.parse("2012-11-29T13:15:18.176-0100"); assertEquals(14,cal.get(Calendar.HOUR_OF_DAY)); }