@Test public void testValidateElectronicTaxReceiptAccessKey() throws Exception { Assert.assertTrue(validateElectronicTaxReceiptAccessKey("35151222843642000195650010000018121400020216")); Assert.assertTrue(validateElectronicTaxReceiptAccessKey("3515 1222 8436 4200 0195 6500 1000 0018 1214 0002 0216")); Assert.assertFalse(validateElectronicTaxReceiptAccessKey("3515 1222 8436 2400 0195 6650 1000 0018 1214 0002 0216")); Assert.assertTrue(validateElectronicTaxReceiptAccessKey("3515 0922 8436 4200 0195 6500 1000 0000 2314 0000 2313")); Assert.assertFalse(validateElectronicTaxReceiptAccessKey("3500 1222 8436 2400 0195 6650 1000 0018 1214 0002 0216")); Assert.assertTrue(validateElectronicTaxReceiptAccessKey("3500 0922 8436 4200 0195 6500 1000 0000 2314 0000 2313")); Assert.assertTrue(validateElectronicTaxReceiptAccessKey("3599 0922 8436 4200 0195 6500 1000 0000 2314 0000 2313")); Assert.assertFalse(validateElectronicTaxReceiptAccessKey("3599 1322 8436 4200 0195 6500 1000 0000 2314 0000 2313")); Assert.assertFalse(validateElectronicTaxReceiptAccessKey("3599 1232 8436 4200 0195 6500 1000 0000 2314 0000 2313")); }
public static boolean validateElectronicTaxReceiptAccessKey(String accessKey) { boolean ret = true; accessKey = removeNonNumeric(accessKey); DateFormat df = new SimpleDateFormat("yyMM"); if (accessKey.length() != 44) ret = false; if (!isNumeric(accessKey)) ret = false; if (!Arrays.asList(Constants.IBGE_UF_CODES).contains(accessKey.substring(0, 2))) ret = false; try { Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTime(df.parse(accessKey.substring(2, 6))); Assert.isTrue(calendar.get(Calendar.MONTH) == Integer.parseInt(accessKey.substring(4, 6)) - 1); } catch (Exception e) { ret = false; } if (!validateCNPJ(accessKey.substring(6, 20))) ret = false; return ret; }
StringUtils { public static boolean validateElectronicTaxReceiptAccessKey(String accessKey) { boolean ret = true; accessKey = removeNonNumeric(accessKey); DateFormat df = new SimpleDateFormat("yyMM"); if (accessKey.length() != 44) ret = false; if (!isNumeric(accessKey)) ret = false; if (!Arrays.asList(Constants.IBGE_UF_CODES).contains(accessKey.substring(0, 2))) ret = false; try { Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTime(df.parse(accessKey.substring(2, 6))); Assert.isTrue(calendar.get(Calendar.MONTH) == Integer.parseInt(accessKey.substring(4, 6)) - 1); } catch (Exception e) { ret = false; } if (!validateCNPJ(accessKey.substring(6, 20))) ret = false; return ret; } }
StringUtils { public static boolean validateElectronicTaxReceiptAccessKey(String accessKey) { boolean ret = true; accessKey = removeNonNumeric(accessKey); DateFormat df = new SimpleDateFormat("yyMM"); if (accessKey.length() != 44) ret = false; if (!isNumeric(accessKey)) ret = false; if (!Arrays.asList(Constants.IBGE_UF_CODES).contains(accessKey.substring(0, 2))) ret = false; try { Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTime(df.parse(accessKey.substring(2, 6))); Assert.isTrue(calendar.get(Calendar.MONTH) == Integer.parseInt(accessKey.substring(4, 6)) - 1); } catch (Exception e) { ret = false; } if (!validateCNPJ(accessKey.substring(6, 20))) ret = false; return ret; }  }
StringUtils { public static boolean validateElectronicTaxReceiptAccessKey(String accessKey) { boolean ret = true; accessKey = removeNonNumeric(accessKey); DateFormat df = new SimpleDateFormat("yyMM"); if (accessKey.length() != 44) ret = false; if (!isNumeric(accessKey)) ret = false; if (!Arrays.asList(Constants.IBGE_UF_CODES).contains(accessKey.substring(0, 2))) ret = false; try { Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTime(df.parse(accessKey.substring(2, 6))); Assert.isTrue(calendar.get(Calendar.MONTH) == Integer.parseInt(accessKey.substring(4, 6)) - 1); } catch (Exception e) { ret = false; } if (!validateCNPJ(accessKey.substring(6, 20))) ret = false; return ret; }  static boolean isNumeric(String maybeNumeric); static boolean validateCNPJ(String CNPJ); static String reverseString(String str); static void appendVarArgs(StringBuilder sb, String... strings); static boolean validateElectronicTaxReceiptAccessKey(String accessKey); static String removeNonNumeric(String text); }
StringUtils { public static boolean validateElectronicTaxReceiptAccessKey(String accessKey) { boolean ret = true; accessKey = removeNonNumeric(accessKey); DateFormat df = new SimpleDateFormat("yyMM"); if (accessKey.length() != 44) ret = false; if (!isNumeric(accessKey)) ret = false; if (!Arrays.asList(Constants.IBGE_UF_CODES).contains(accessKey.substring(0, 2))) ret = false; try { Calendar calendar = Calendar.getInstance(); calendar.setLenient(false); calendar.setTime(df.parse(accessKey.substring(2, 6))); Assert.isTrue(calendar.get(Calendar.MONTH) == Integer.parseInt(accessKey.substring(4, 6)) - 1); } catch (Exception e) { ret = false; } if (!validateCNPJ(accessKey.substring(6, 20))) ret = false; return ret; }  static boolean isNumeric(String maybeNumeric); static boolean validateCNPJ(String CNPJ); static String reverseString(String str); static void appendVarArgs(StringBuilder sb, String... strings); static boolean validateElectronicTaxReceiptAccessKey(String accessKey); static String removeNonNumeric(String text);  }