{"id":"01757fe8877e4e2e","repo":"google/gson","slug":"invalid-number-value","errorCode":null,"errorMessage":"Invalid number: {value}","messagePattern":"Invalid number: (.+?)","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java","lineNumber":267,"sourceCode":"   * @param value the string to parse\n   * @param beginIndex the start index for the integer in the string\n   * @param endIndex the end index for the integer in the string\n   * @return the int\n   * @throws NumberFormatException if the value is not a number\n   */\n  private static int parseInt(String value, int beginIndex, int endIndex)\n      throws NumberFormatException {\n    if (beginIndex < 0 || endIndex > value.length() || beginIndex > endIndex) {\n      throw new NumberFormatException(value);\n    }\n    // use same logic as in Integer.parseInt() but less generic we're not supporting negative values\n    int i = beginIndex;\n    int result = 0;\n    int digit;\n    if (i < endIndex) {\n      digit = Character.digit(value.charAt(i++), 10);\n      if (digit < 0) {\n        throw new NumberFormatException(\"Invalid number: \" + value);\n      }\n      result = -digit;\n    }\n    while (i < endIndex) {\n      digit = Character.digit(value.charAt(i++), 10);\n      if (digit < 0) {\n        throw new NumberFormatException(\"Invalid number: \" + value);\n      }\n      result *= 10;\n      result -= digit;\n    }\n    return -result;\n  }\n}\n","sourceCodeStart":249,"sourceCodeEnd":282,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java#L249-L282","documentation":"Thrown internally by UtcDateTypeAdapter.parseInt() when the FIRST character of the integer field being parsed is not a decimal digit (Character.digit returns < 0). This means a non-numeric character appears where the parser expects the start of a number (e.g. a letter where the year should be). It is a NumberFormatException that flows up into the umbrella ParseException (error 7).","triggerScenarios":"Date strings like \"abcd-01-01T00:00:00Z\" where the year contains letters; separators appearing where digits are expected; locale-specific month names; corrupted numeric fields.","commonSituations":"Garbled payloads; dates formatted with month names (\"Jan\") instead of numeric months; data-entry or transport corruption introducing non-digits; wrong field ordering.","solutions":["Ensure all numeric date fields (year, month, day, hour, minute, second, ms) contain only decimal digits in fixed widths.","Pre-validate with a regex that pins digit positions, e.g. ^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.*.","Use java.time with a DateTimeFormatter whose pattern matches the actual input (e.g. MMM for month names) via a custom adapter.","Sanitize/normalize the input at the source."],"exampleFix":"// before: non-digit in numeric position\nString json = \"\\\"YYYY-01-01T00:00:00Z\\\"\";\nDate d = gson.fromJson(json, Date.class); // JsonParseException via NumberFormatException\n\n// after: numeric fields\nString json = \"\\\"2024-01-01T00:00:00Z\\\"\";\nDate d = gson.fromJson(json, Date.class);","handlingStrategy":"validation","validationCode":"// Ensure numeric date fields contain only digits at the right positions\nstatic boolean digitsOnlyAt(String date, int start, int end) {\n  if (date.length() < end) return false;\n  for (int i = start; i < end; i++) {\n    if (!Character.isDigit(date.charAt(i))) return false;\n  }\n  return true;\n}\nstatic boolean validIsoNumbers(String d) {\n  return digitsOnlyAt(d, 0, 4) && digitsOnlyAt(d, 5, 7) && digitsOnlyAt(d, 8, 10);\n}","typeGuard":"static boolean startsWithDigit(String date) {\n  return date != null && !date.isEmpty() && Character.isDigit(date.charAt(0));\n}","tryCatchPattern":"try {\n  Date d = gson.fromJson(json, Date.class);\n} catch (JsonParseException e) {\n  if (e.getCause() != null && e.getCause().getCause() instanceof NumberFormatException) {\n    // non-digit in numeric position; sanitize or reject\n  } else throw e;\n}","preventionTips":["Validate that numeric date fields contain only decimal digits before parsing.","Use a strict regex pinning digit positions: ^\\\\d{4}-\\\\d{2}-\\\\d{2}T\\\\d{2}:\\\\d{2}:\\\\d{2}.*.","Reject non-ISO formats (month names, etc.) at the validation boundary.","Normalize producer output to numeric ISO-8601."],"tags":["date","iso8601","utc-adapter","parsing","number-format","non-digit"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}