google/gson · error · NumberFormatException
{value}
Error message
{value} What it means
Thrown internally by UtcDateTypeAdapter.parseInt() when the requested character range is out of bounds: beginIndex < 0, endIndex > string length, or beginIndex > endIndex. This indicates the date string is shorter than the parser expected at that field (e.g. a 2-digit month where the string ended early). It is a NumberFormatException whose message is the raw value; it propagates up and becomes part of the umbrella ParseException (error 7).
Source
Thrown at extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java:258
* @return true if the expected character exist at the given offset
*/
private static boolean checkOffset(String value, int offset, char expected) {
return (offset < value.length()) && (value.charAt(offset) == expected);
}
/**
* Parse an integer located between 2 given offsets in a string
*
* @param value the string to parse
* @param beginIndex the start index for the integer in the string
* @param endIndex the end index for the integer in the string
* @return the int
* @throws NumberFormatException if the value is not a number
*/
private static int parseInt(String value, int beginIndex, int endIndex)
throws NumberFormatException {
if (beginIndex < 0 || endIndex > value.length() || beginIndex > endIndex) {
throw new NumberFormatException(value);
}
// use same logic as in Integer.parseInt() but less generic we're not supporting negative values
int i = beginIndex;
int result = 0;
int digit;
if (i < endIndex) {
digit = Character.digit(value.charAt(i++), 10);
if (digit < 0) {
throw new NumberFormatException("Invalid number: " + value);
}
result = -digit;
}
while (i < endIndex) {
digit = Character.digit(value.charAt(i++), 10);
if (digit < 0) {
throw new NumberFormatException("Invalid number: " + value);
}
result *= 10;View on GitHub (pinned to 8b8628c656)
Solutions
- Ensure the date string is complete and conforms to ISO-8601 length expectations (year=4, month=2, day=2, etc.).
- Validate length/format with a regex before parsing (e.g. ^\d{4}-?\d{2}-?\d{2}.*).
- Switch to a custom adapter using java.time with a lenient or optional-section pattern if input is variable.
- Inspect the raw input shown in the wrapped ParseException to see where truncation occurred.
Example fix
// before: truncated date String json = "\"2024\""; Date d = gson.fromJson(json, Date.class); // ultimately JsonParseException // after: full ISO-8601 date String json = "\"2024-01-01T00:00:00Z\""; Date d = gson.fromJson(json, Date.class);
Defensive patterns
Strategy: validation
Validate before calling
// Reject truncated date strings before parsing
static void requireMinLength(String date, int min) {
if (date == null || date.length() < min) {
throw new IllegalArgumentException("Date too short (expected >= " + min + "): " + date);
}
} Type guard
static boolean isCompleteIsoDate(String date) {
// basic completeness check for yyyy-MM-ddThh:mm:ss[Z|+HH:mm]
return date != null && date.length() >= 20 && date.matches("^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.*");
} Try / catch
try {
Date d = gson.fromJson(json, Date.class);
} catch (JsonParseException e) {
Throwable c = e.getCause();
if (c != null && c.getMessage() != null && c.getCause() instanceof NumberFormatException) {
// truncated input; reject or pad/retry
} else throw e;
} Prevention
- Ensure producer emits full ISO-8601 strings (no truncation).
- Validate minimum length and digit-position regex before parsing.
- Guard transport/serialization layers against trimming or dropping characters.
- Use java.time with optional sections if some components are legitimately absent.
When it happens
Trigger: A date string too short for the field being extracted, e.g. "2024" when the parser tries to read 2 more chars for the month; truncated payloads; off-by-one slicing from upstream processing. Ultimately surfaced as JsonParseException via the ParseException wrapper.
Common situations: Truncated timestamps; strings that were accidentally trimmed; non-ISO formats that happen to parse partially then run out of characters; encoding issues dropping bytes.
Related errors
- Invalid number: {value}
- No time zone indicator
- Invalid time zone indicator {timezoneIndicator}
- Failed to parse date [{input}]: {fail.getMessage()}
- Array must have size 1, but has size {size}
AI-assisted analysis of google/gson@8b8628c656 (2026-08-04).
Data as JSON: /data/errors/e8b0197508e6b2cb.json.
Report an issue: GitHub.