@Test public void parse() { Calendar cal = ISO8601.parse("2017-03-27T23:57:42.987Z"); assertThat(cal).isNotNull(); assertThat(cal.get(Calendar.YEAR)).isEqualTo(2017); cal = ISO8601.parse("2017-03-27T23:57:42.987+07:00"); assertThat(cal).isNotNull(); cal = ISO8601.parse("2017-03-27T23:57:42.987-03:30"); assertThat(cal).isNotNull(); }
public static Calendar parse(String text) { if (text == null) { throw new IllegalArgumentException("argument can not be null"); } char sign; int start; if (text.startsWith("-")) { sign = '-'; start = 1; } else if (text.startsWith("+")) { sign = '+'; start = 1; } else { sign = '+'; start = 0; } int year, month, day, hour, min, sec, ms; String tzID; try { year = Integer.parseInt(text.substring(start, start + 4)); start += 4; if (text.charAt(start) != '-') { return null; } start++; month = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '-') { return null; } start++; day = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != 'T') { return null; } start++; hour = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; min = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; sec = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '.') { return null; } start++; ms = Integer.parseInt(text.substring(start, start + 3)); start += 3; if (text.charAt(start) == '+' || text.charAt(start) == '-') { tzID = "GMT" + text.substring(start); } else if (text.substring(start).equals("Z")) { tzID = "GMT"; } else { return null; } } catch (IndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } TimeZone tz = TimeZone.getTimeZone(tzID); if (!tz.getID().equals(tzID)) { return null; } Calendar cal = Calendar.getInstance(tz); cal.setLenient(false); if (sign == '-' || year == 0) { cal.set(Calendar.YEAR, year + 1); cal.set(Calendar.ERA, GregorianCalendar.BC); } else { cal.set(Calendar.YEAR, year); cal.set(Calendar.ERA, GregorianCalendar.AD); } cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND, ms); try { cal.getTime(); getYear(cal); } catch (IllegalArgumentException e) { return null; } return cal; }
ISO8601 { public static Calendar parse(String text) { if (text == null) { throw new IllegalArgumentException("argument can not be null"); } char sign; int start; if (text.startsWith("-")) { sign = '-'; start = 1; } else if (text.startsWith("+")) { sign = '+'; start = 1; } else { sign = '+'; start = 0; } int year, month, day, hour, min, sec, ms; String tzID; try { year = Integer.parseInt(text.substring(start, start + 4)); start += 4; if (text.charAt(start) != '-') { return null; } start++; month = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '-') { return null; } start++; day = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != 'T') { return null; } start++; hour = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; min = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; sec = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '.') { return null; } start++; ms = Integer.parseInt(text.substring(start, start + 3)); start += 3; if (text.charAt(start) == '+' || text.charAt(start) == '-') { tzID = "GMT" + text.substring(start); } else if (text.substring(start).equals("Z")) { tzID = "GMT"; } else { return null; } } catch (IndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } TimeZone tz = TimeZone.getTimeZone(tzID); if (!tz.getID().equals(tzID)) { return null; } Calendar cal = Calendar.getInstance(tz); cal.setLenient(false); if (sign == '-' || year == 0) { cal.set(Calendar.YEAR, year + 1); cal.set(Calendar.ERA, GregorianCalendar.BC); } else { cal.set(Calendar.YEAR, year); cal.set(Calendar.ERA, GregorianCalendar.AD); } cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND, ms); try { cal.getTime(); getYear(cal); } catch (IllegalArgumentException e) { return null; } return cal; } }
ISO8601 { public static Calendar parse(String text) { if (text == null) { throw new IllegalArgumentException("argument can not be null"); } char sign; int start; if (text.startsWith("-")) { sign = '-'; start = 1; } else if (text.startsWith("+")) { sign = '+'; start = 1; } else { sign = '+'; start = 0; } int year, month, day, hour, min, sec, ms; String tzID; try { year = Integer.parseInt(text.substring(start, start + 4)); start += 4; if (text.charAt(start) != '-') { return null; } start++; month = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '-') { return null; } start++; day = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != 'T') { return null; } start++; hour = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; min = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; sec = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '.') { return null; } start++; ms = Integer.parseInt(text.substring(start, start + 3)); start += 3; if (text.charAt(start) == '+' || text.charAt(start) == '-') { tzID = "GMT" + text.substring(start); } else if (text.substring(start).equals("Z")) { tzID = "GMT"; } else { return null; } } catch (IndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } TimeZone tz = TimeZone.getTimeZone(tzID); if (!tz.getID().equals(tzID)) { return null; } Calendar cal = Calendar.getInstance(tz); cal.setLenient(false); if (sign == '-' || year == 0) { cal.set(Calendar.YEAR, year + 1); cal.set(Calendar.ERA, GregorianCalendar.BC); } else { cal.set(Calendar.YEAR, year); cal.set(Calendar.ERA, GregorianCalendar.AD); } cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND, ms); try { cal.getTime(); getYear(cal); } catch (IllegalArgumentException e) { return null; } return cal; }  }
ISO8601 { public static Calendar parse(String text) { if (text == null) { throw new IllegalArgumentException("argument can not be null"); } char sign; int start; if (text.startsWith("-")) { sign = '-'; start = 1; } else if (text.startsWith("+")) { sign = '+'; start = 1; } else { sign = '+'; start = 0; } int year, month, day, hour, min, sec, ms; String tzID; try { year = Integer.parseInt(text.substring(start, start + 4)); start += 4; if (text.charAt(start) != '-') { return null; } start++; month = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '-') { return null; } start++; day = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != 'T') { return null; } start++; hour = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; min = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; sec = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '.') { return null; } start++; ms = Integer.parseInt(text.substring(start, start + 3)); start += 3; if (text.charAt(start) == '+' || text.charAt(start) == '-') { tzID = "GMT" + text.substring(start); } else if (text.substring(start).equals("Z")) { tzID = "GMT"; } else { return null; } } catch (IndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } TimeZone tz = TimeZone.getTimeZone(tzID); if (!tz.getID().equals(tzID)) { return null; } Calendar cal = Calendar.getInstance(tz); cal.setLenient(false); if (sign == '-' || year == 0) { cal.set(Calendar.YEAR, year + 1); cal.set(Calendar.ERA, GregorianCalendar.BC); } else { cal.set(Calendar.YEAR, year); cal.set(Calendar.ERA, GregorianCalendar.AD); } cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND, ms); try { cal.getTime(); getYear(cal); } catch (IllegalArgumentException e) { return null; } return cal; }  static Date parseDate(String text); static String formatDate(Date date); static Calendar parse(String text); static String format(Calendar cal); static int getYear(Calendar cal); }
ISO8601 { public static Calendar parse(String text) { if (text == null) { throw new IllegalArgumentException("argument can not be null"); } char sign; int start; if (text.startsWith("-")) { sign = '-'; start = 1; } else if (text.startsWith("+")) { sign = '+'; start = 1; } else { sign = '+'; start = 0; } int year, month, day, hour, min, sec, ms; String tzID; try { year = Integer.parseInt(text.substring(start, start + 4)); start += 4; if (text.charAt(start) != '-') { return null; } start++; month = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '-') { return null; } start++; day = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != 'T') { return null; } start++; hour = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; min = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != ':') { return null; } start++; sec = Integer.parseInt(text.substring(start, start + 2)); start += 2; if (text.charAt(start) != '.') { return null; } start++; ms = Integer.parseInt(text.substring(start, start + 3)); start += 3; if (text.charAt(start) == '+' || text.charAt(start) == '-') { tzID = "GMT" + text.substring(start); } else if (text.substring(start).equals("Z")) { tzID = "GMT"; } else { return null; } } catch (IndexOutOfBoundsException e) { return null; } catch (NumberFormatException e) { return null; } TimeZone tz = TimeZone.getTimeZone(tzID); if (!tz.getID().equals(tzID)) { return null; } Calendar cal = Calendar.getInstance(tz); cal.setLenient(false); if (sign == '-' || year == 0) { cal.set(Calendar.YEAR, year + 1); cal.set(Calendar.ERA, GregorianCalendar.BC); } else { cal.set(Calendar.YEAR, year); cal.set(Calendar.ERA, GregorianCalendar.AD); } cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, day); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, min); cal.set(Calendar.SECOND, sec); cal.set(Calendar.MILLISECOND, ms); try { cal.getTime(); getYear(cal); } catch (IllegalArgumentException e) { return null; } return cal; }  static Date parseDate(String text); static String formatDate(Date date); static Calendar parse(String text); static String format(Calendar cal); static int getYear(Calendar cal);  }