@Test public void shouldParseOffWithVariousDateFormats() throws ConverterException { String[] variousDates = { "2018-08-08T08:45:00-08", "2018-12-10T08:45", "2018-12-10T08:45-01:30", "2018-12-10T08:45:30", "2018-12-10T08:45:30.999999999", "2018-12-10T08:45:30.999999999-0800", "2018-12-10T08:45:30.999999999+08:00", "2018-12-10T", "2018-12-10T08", "2018-12-10T08-01", "2018-12-10T08-0130", }; Calendar calendar; for (String date:variousDates) { try { calendar = isoTocal(date); } catch (Exception ex) { calendar = null; } assertNotNull(date+ " does not follow ISO-8601 format, could not be parsed.", calendar); } }
public static Calendar isoTocal(String isoStr) throws ConverterException { Calendar out; Matcher tzMatcher; String tzStr; ZoneOffset dtZone; if(isoStr == null){ throw new ConverterException(new NullPointerException()); } try { tzMatcher = tzPattern.matcher(isoStr); if(tzMatcher.find()) { tzStr = String.format("%s%s",tzMatcher.group(1),tzMatcher.group(2)); dtZone = ZoneOffset.of(tzStr); isoStr = StringUtils.removeEnd(isoStr,tzStr); isoStr = isoStr + dtZone.toString(); if(isoStr.length() == 19){ StringBuffer buffer = new StringBuffer(isoStr); buffer.insert(isoStr.lastIndexOf('-'),defaultMinutes); isoStr = buffer.toString(); } }else{ if (!isoStr.contains("T")) { isoStr = String.format("%sT%s", isoStr, defaultTime); } else { if (isoStr.endsWith("T")){ isoStr = String.format("%s%s", isoStr, defaultTime); } else { if(Pattern.matches(".*T[0-9]{2}$", isoStr)){ isoStr = isoStr + defaultMinutes; } } } dtZone = ZoneOffset.UTC; } OffsetDateTime dateTime = OffsetDateTime.of(LocalDateTime.parse(isoStr, DateTimeFormatter.ISO_DATE_TIME),dtZone); out = GregorianCalendar.from(ZonedDateTime.from(dateTime)); } catch (Exception ex) { throw new ConverterException(ex); } return out; }
DateTimeConverters { public static Calendar isoTocal(String isoStr) throws ConverterException { Calendar out; Matcher tzMatcher; String tzStr; ZoneOffset dtZone; if(isoStr == null){ throw new ConverterException(new NullPointerException()); } try { tzMatcher = tzPattern.matcher(isoStr); if(tzMatcher.find()) { tzStr = String.format("%s%s",tzMatcher.group(1),tzMatcher.group(2)); dtZone = ZoneOffset.of(tzStr); isoStr = StringUtils.removeEnd(isoStr,tzStr); isoStr = isoStr + dtZone.toString(); if(isoStr.length() == 19){ StringBuffer buffer = new StringBuffer(isoStr); buffer.insert(isoStr.lastIndexOf('-'),defaultMinutes); isoStr = buffer.toString(); } }else{ if (!isoStr.contains("T")) { isoStr = String.format("%sT%s", isoStr, defaultTime); } else { if (isoStr.endsWith("T")){ isoStr = String.format("%s%s", isoStr, defaultTime); } else { if(Pattern.matches(".*T[0-9]{2}$", isoStr)){ isoStr = isoStr + defaultMinutes; } } } dtZone = ZoneOffset.UTC; } OffsetDateTime dateTime = OffsetDateTime.of(LocalDateTime.parse(isoStr, DateTimeFormatter.ISO_DATE_TIME),dtZone); out = GregorianCalendar.from(ZonedDateTime.from(dateTime)); } catch (Exception ex) { throw new ConverterException(ex); } return out; } }
DateTimeConverters { public static Calendar isoTocal(String isoStr) throws ConverterException { Calendar out; Matcher tzMatcher; String tzStr; ZoneOffset dtZone; if(isoStr == null){ throw new ConverterException(new NullPointerException()); } try { tzMatcher = tzPattern.matcher(isoStr); if(tzMatcher.find()) { tzStr = String.format("%s%s",tzMatcher.group(1),tzMatcher.group(2)); dtZone = ZoneOffset.of(tzStr); isoStr = StringUtils.removeEnd(isoStr,tzStr); isoStr = isoStr + dtZone.toString(); if(isoStr.length() == 19){ StringBuffer buffer = new StringBuffer(isoStr); buffer.insert(isoStr.lastIndexOf('-'),defaultMinutes); isoStr = buffer.toString(); } }else{ if (!isoStr.contains("T")) { isoStr = String.format("%sT%s", isoStr, defaultTime); } else { if (isoStr.endsWith("T")){ isoStr = String.format("%s%s", isoStr, defaultTime); } else { if(Pattern.matches(".*T[0-9]{2}$", isoStr)){ isoStr = isoStr + defaultMinutes; } } } dtZone = ZoneOffset.UTC; } OffsetDateTime dateTime = OffsetDateTime.of(LocalDateTime.parse(isoStr, DateTimeFormatter.ISO_DATE_TIME),dtZone); out = GregorianCalendar.from(ZonedDateTime.from(dateTime)); } catch (Exception ex) { throw new ConverterException(ex); } return out; }  }
DateTimeConverters { public static Calendar isoTocal(String isoStr) throws ConverterException { Calendar out; Matcher tzMatcher; String tzStr; ZoneOffset dtZone; if(isoStr == null){ throw new ConverterException(new NullPointerException()); } try { tzMatcher = tzPattern.matcher(isoStr); if(tzMatcher.find()) { tzStr = String.format("%s%s",tzMatcher.group(1),tzMatcher.group(2)); dtZone = ZoneOffset.of(tzStr); isoStr = StringUtils.removeEnd(isoStr,tzStr); isoStr = isoStr + dtZone.toString(); if(isoStr.length() == 19){ StringBuffer buffer = new StringBuffer(isoStr); buffer.insert(isoStr.lastIndexOf('-'),defaultMinutes); isoStr = buffer.toString(); } }else{ if (!isoStr.contains("T")) { isoStr = String.format("%sT%s", isoStr, defaultTime); } else { if (isoStr.endsWith("T")){ isoStr = String.format("%s%s", isoStr, defaultTime); } else { if(Pattern.matches(".*T[0-9]{2}$", isoStr)){ isoStr = isoStr + defaultMinutes; } } } dtZone = ZoneOffset.UTC; } OffsetDateTime dateTime = OffsetDateTime.of(LocalDateTime.parse(isoStr, DateTimeFormatter.ISO_DATE_TIME),dtZone); out = GregorianCalendar.from(ZonedDateTime.from(dateTime)); } catch (Exception ex) { throw new ConverterException(ex); } return out; }  static Calendar isoTocalNoExc(String isoStr); static Calendar isoTocal(String isoStr); static String calToiso(Calendar cal); static String calToisoNoExc(Calendar cal); static void main(String[] ags); }
DateTimeConverters { public static Calendar isoTocal(String isoStr) throws ConverterException { Calendar out; Matcher tzMatcher; String tzStr; ZoneOffset dtZone; if(isoStr == null){ throw new ConverterException(new NullPointerException()); } try { tzMatcher = tzPattern.matcher(isoStr); if(tzMatcher.find()) { tzStr = String.format("%s%s",tzMatcher.group(1),tzMatcher.group(2)); dtZone = ZoneOffset.of(tzStr); isoStr = StringUtils.removeEnd(isoStr,tzStr); isoStr = isoStr + dtZone.toString(); if(isoStr.length() == 19){ StringBuffer buffer = new StringBuffer(isoStr); buffer.insert(isoStr.lastIndexOf('-'),defaultMinutes); isoStr = buffer.toString(); } }else{ if (!isoStr.contains("T")) { isoStr = String.format("%sT%s", isoStr, defaultTime); } else { if (isoStr.endsWith("T")){ isoStr = String.format("%s%s", isoStr, defaultTime); } else { if(Pattern.matches(".*T[0-9]{2}$", isoStr)){ isoStr = isoStr + defaultMinutes; } } } dtZone = ZoneOffset.UTC; } OffsetDateTime dateTime = OffsetDateTime.of(LocalDateTime.parse(isoStr, DateTimeFormatter.ISO_DATE_TIME),dtZone); out = GregorianCalendar.from(ZonedDateTime.from(dateTime)); } catch (Exception ex) { throw new ConverterException(ex); } return out; }  static Calendar isoTocalNoExc(String isoStr); static Calendar isoTocal(String isoStr); static String calToiso(Calendar cal); static String calToisoNoExc(Calendar cal); static void main(String[] ags); static final String defaultMinutes; }