zhisheng17/flink-learning · error · IllegalArgumentException
Time interval unit '${unit}' does not match any of the recog
Error message
Time interval unit '${unit}' does not match any of the recognized units: ${TimeUnit.getAllUnits()} What it means
TimeUtils.parseDuration parses human-readable duration strings like '100 ms' or '5 s'. It throws this IllegalArgumentException when the unit token after the numeric value does not match any recognized time unit (ms, s, m, min, h, d etc.) as checked by matchTimeUnit against TimeUnit constants.
Source
Thrown at flink-learning-core/src/main/java/com/zhisheng/core/utils/TimeUtils.java:80
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The value '" + number +
"' cannot be re represented as 64bit number (numeric overflow).");
}
final long multiplier;
if (unit.isEmpty()) {
multiplier = 1L;
} else {
if (matchTimeUnit(unit, TimeUnit.MILLISECONDS)) {
multiplier = 1L;
} else if (matchTimeUnit(unit, TimeUnit.SECONDS)) {
multiplier = 1000L;
} else if (matchTimeUnit(unit, TimeUnit.MINUTES)) {
multiplier = 1000L * 60L;
} else if (matchTimeUnit(unit, TimeUnit.HOURS)) {
multiplier = 1000L * 60L * 60L;
} else {
throw new IllegalArgumentException("Time interval unit '" + unit +
"' does not match any of the recognized units: " + TimeUnit.getAllUnits());
}
}
final long result = value * multiplier;
// check for overflow
if (result / multiplier != value) {
throw new IllegalArgumentException("The value '" + text +
"' cannot be re represented as 64bit number of bytes (numeric overflow).");
}
return Duration.ofMillis(result);
}
private static boolean matchTimeUnit(String text, TimeUnit unit) {
return text.equals(unit.getUnit());
}View on GitHub (pinned to d731cee761)
Solutions
- Fix the duration string to use a recognized unit: '100 ms', '5 s', '10 min', '2 h', '1 d'.
- Check TimeUtils/matchTimeUnit for the exact accepted unit spellings (they accept common prefixes/suffixes of TimeUnit).
- If parsing user input, pre-validate the unit against the accepted set and produce a friendlier message.
- Fall back to plain numeric values (interpreted as milliseconds) where the config allows it.
Example fix
// before
TimeUtils.parseDuration("30secs");
// after
TimeUtils.parseDuration("30 s"); Defensive patterns
Strategy: validation
Validate before calling
private static final java.util.regex.Pattern DURATION = java.util.regex.Pattern.compile("^\\s*\\d+\\s*(ms|s|m|min|h|d)\\s*$");
static void checkDuration(String text) {
if (text == null || !DURATION.matcher(text).matches())
throw new IllegalArgumentException("Unrecognized duration: " + text);
} Try / catch
try {
Duration d = TimeUtils.parseDuration(cfg.get("interval"));
} catch (IllegalArgumentException e) {
log.warn("Bad duration '{}', falling back to 1s", cfg.get("interval"), e);
d = Duration.ofSeconds(1);
} Prevention
- Only use units accepted by TimeUtils (ms, s, m/min, h, d)
- Trim whitespace and validate duration strings before parsing
- Centralize duration parsing in one config helper with pre-validation
- Prefer plain milliseconds in config when unit syntax is uncertain
When it happens
Trigger: Calling TimeUtils.parseDuration with a string whose unit suffix matches no known unit, e.g. '30sec', '5 minutes ' with stray characters, '10msx', or an empty unit after the number.
Common situations: Typo in Flink config values like 'task.cancellation-interval: 10secs'; locale/case mismatches; copy-pasted config from docs of another framework with different unit abbreviations.
Understand the failure class
Background: "invalid duration" / "failed to parse duration": why your timeout, interval, or TTL string is rejected and which formats each library accepts — this error's family across 32 libraries.
Related errors
- text does not start with a number
- The value '${number}' cannot be re represented as 64bit numb
- The value '${text}' cannot be re represented as 64bit number
- invalid elasticsearch hosts format
- invalid elasticsearch hosts format
AI-assisted analysis of zhisheng17/flink-learning@d731cee761 (2026-09-06).
Data as JSON: /api/errors/b94728ab61fde752.
Report an issue: GitHub.