@Test public void testItuX680() throws Exception { Date expected = Date.from(LocalDateTime.parse("1985-11-06T21:06:27.3").atZone(ZoneId.systemDefault()).toInstant()); Assert.assertEquals(expected, LdapGeneralizedTimeUtil.generalizedTimeToDate("19851106210627.3")); expected = Date.from(ZonedDateTime.parse("1985-11-06T21:06:27.3+00:00").toInstant()); Assert.assertEquals(expected, LdapGeneralizedTimeUtil.generalizedTimeToDate("19851106210627.3Z")); expected = Date.from(ZonedDateTime.parse("1985-11-06T21:06:27.3-05:00").toInstant()); Assert.assertEquals(expected, LdapGeneralizedTimeUtil.generalizedTimeToDate("19851106210627.3-0500")); }
static Date generalizedTimeToDate(String generalized) throws ParseException { String[] parts = generalized.split("[Z+-]"); String[] timeFraction = parts[0].split("[.,]"); String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); if (time.length() >= 14) calendar.set(Calendar.SECOND, Integer.parseInt(time.substring(12, 14))); if (timeFraction.length >= 2) { double fraction = Double.parseDouble("0." + timeFraction[1]); if (time.length() >= 14) { calendar.set(Calendar.MILLISECOND, (int) Math.round(fraction * 1000)); } else if (time.length() >= 12) { calendar.set(Calendar.SECOND, (int) Math.round(fraction * 60)); } else { calendar.set(Calendar.MINUTE, (int) Math.round(fraction * 60)); } } if (generalized.length() > parts[0].length()) { char delimiter = generalized.charAt(parts[0].length()); if (delimiter == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { calendar.setTimeZone(TimeZone.getTimeZone("GMT" + delimiter + parts[1])); } } return calendar.getTime(); }
LdapGeneralizedTimeUtil { static Date generalizedTimeToDate(String generalized) throws ParseException { String[] parts = generalized.split("[Z+-]"); String[] timeFraction = parts[0].split("[.,]"); String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); if (time.length() >= 14) calendar.set(Calendar.SECOND, Integer.parseInt(time.substring(12, 14))); if (timeFraction.length >= 2) { double fraction = Double.parseDouble("0." + timeFraction[1]); if (time.length() >= 14) { calendar.set(Calendar.MILLISECOND, (int) Math.round(fraction * 1000)); } else if (time.length() >= 12) { calendar.set(Calendar.SECOND, (int) Math.round(fraction * 60)); } else { calendar.set(Calendar.MINUTE, (int) Math.round(fraction * 60)); } } if (generalized.length() > parts[0].length()) { char delimiter = generalized.charAt(parts[0].length()); if (delimiter == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { calendar.setTimeZone(TimeZone.getTimeZone("GMT" + delimiter + parts[1])); } } return calendar.getTime(); } }
LdapGeneralizedTimeUtil { static Date generalizedTimeToDate(String generalized) throws ParseException { String[] parts = generalized.split("[Z+-]"); String[] timeFraction = parts[0].split("[.,]"); String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); if (time.length() >= 14) calendar.set(Calendar.SECOND, Integer.parseInt(time.substring(12, 14))); if (timeFraction.length >= 2) { double fraction = Double.parseDouble("0." + timeFraction[1]); if (time.length() >= 14) { calendar.set(Calendar.MILLISECOND, (int) Math.round(fraction * 1000)); } else if (time.length() >= 12) { calendar.set(Calendar.SECOND, (int) Math.round(fraction * 60)); } else { calendar.set(Calendar.MINUTE, (int) Math.round(fraction * 60)); } } if (generalized.length() > parts[0].length()) { char delimiter = generalized.charAt(parts[0].length()); if (delimiter == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { calendar.setTimeZone(TimeZone.getTimeZone("GMT" + delimiter + parts[1])); } } return calendar.getTime(); }  }
LdapGeneralizedTimeUtil { static Date generalizedTimeToDate(String generalized) throws ParseException { String[] parts = generalized.split("[Z+-]"); String[] timeFraction = parts[0].split("[.,]"); String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); if (time.length() >= 14) calendar.set(Calendar.SECOND, Integer.parseInt(time.substring(12, 14))); if (timeFraction.length >= 2) { double fraction = Double.parseDouble("0." + timeFraction[1]); if (time.length() >= 14) { calendar.set(Calendar.MILLISECOND, (int) Math.round(fraction * 1000)); } else if (time.length() >= 12) { calendar.set(Calendar.SECOND, (int) Math.round(fraction * 60)); } else { calendar.set(Calendar.MINUTE, (int) Math.round(fraction * 60)); } } if (generalized.length() > parts[0].length()) { char delimiter = generalized.charAt(parts[0].length()); if (delimiter == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { calendar.setTimeZone(TimeZone.getTimeZone("GMT" + delimiter + parts[1])); } } return calendar.getTime(); }   }
LdapGeneralizedTimeUtil { static Date generalizedTimeToDate(String generalized) throws ParseException { String[] parts = generalized.split("[Z+-]"); String[] timeFraction = parts[0].split("[.,]"); String time = timeFraction[0]; Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(0); calendar.setLenient(false); calendar.set(Calendar.YEAR, Integer.parseInt(time.substring(0, 4))); calendar.set(Calendar.MONTH, Integer.parseInt(time.substring(4, 6)) - 1); calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(time.substring(6, 8))); calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.substring(8, 10))); if (time.length() >= 12) calendar.set(Calendar.MINUTE, Integer.parseInt(time.substring(10, 12))); if (time.length() >= 14) calendar.set(Calendar.SECOND, Integer.parseInt(time.substring(12, 14))); if (timeFraction.length >= 2) { double fraction = Double.parseDouble("0." + timeFraction[1]); if (time.length() >= 14) { calendar.set(Calendar.MILLISECOND, (int) Math.round(fraction * 1000)); } else if (time.length() >= 12) { calendar.set(Calendar.SECOND, (int) Math.round(fraction * 60)); } else { calendar.set(Calendar.MINUTE, (int) Math.round(fraction * 60)); } } if (generalized.length() > parts[0].length()) { char delimiter = generalized.charAt(parts[0].length()); if (delimiter == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { calendar.setTimeZone(TimeZone.getTimeZone("GMT" + delimiter + parts[1])); } } return calendar.getTime(); }    }