TheAlgorithms/Java · error · IllegalArgumentException
Invalid unit '{}'. Supported units are: {}
Error message
Invalid unit '{}'. Supported units are: {} What it means
Thrown by TimeConverter.convertTime when either unitFrom or unitTo does not match one of the seven supported units (seconds, minutes, hours, days, weeks, months, years). Lookup is case-insensitive (lowercased via Locale.ROOT) but the string must match exactly after lowercasing. The message interpolates the offending unit and prints the full supported set.
Source
Thrown at src/main/java/com/thealgorithms/conversions/TimeConverter.java:93
throw new IllegalArgumentException("timeValue must be a non-negative number.");
}
TimeUnit from = resolveUnit(unitFrom);
TimeUnit to = resolveUnit(unitTo);
double secondsValue = from.toSeconds(timeValue);
double converted = to.fromSeconds(secondsValue);
return Math.round(converted * 1000.0) / 1000.0;
}
private static TimeUnit resolveUnit(String unit) {
if (unit == null) {
throw new IllegalArgumentException("Unit cannot be null.");
}
TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));
if (resolved == null) {
throw new IllegalArgumentException("Invalid unit '" + unit + "'. Supported units are: " + UNIT_LOOKUP.keySet());
}
return resolved;
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Use exact plural forms from the supported set: seconds, minutes, hours, days, weeks, months, years.
- Normalize the unit string before calling: trim, lowercase, and map abbreviations to canonical names.
- If you need an unsupported unit, extend the enum and UNIT_LOOKUP map or wrap the value with manual conversion.
Example fix
// before
double r = TimeConverter.convertTime(5, "min", "sec");
// after
Map<String,String> aliases = Map.of("min","minutes","sec","seconds","hr","hours","day","days");
String from = aliases.getOrDefault(unitFrom.trim().toLowerCase(), unitFrom.trim().toLowerCase());
double r = TimeConverter.convertTime(5, "minutes", "seconds"); Defensive patterns
Strategy: validation
Validate before calling
private static final Set<String> TIME_UNITS = Set.of("seconds","minutes","hours","days","weeks","months","years");
static String normalizeTimeUnit(String u) {
String n = u == null ? null : u.trim().toLowerCase(Locale.ROOT);
if (!TIME_UNITS.contains(n)) throw new IllegalArgumentException("Unsupported time unit: " + u);
return n;
}
// before convertTime:
String from = normalizeTimeUnit(unitFrom);
String to = normalizeTimeUnit(unitTo); Type guard
static boolean isValidTimeUnit(String unit) {
return unit != null && Set.of("seconds","minutes","hours","days","weeks","weeks","months","years")
.contains(unit.trim().toLowerCase(Locale.ROOT));
} Try / catch
try {
return TimeConverter.convertTime(value, unitFrom, unitTo);
} catch (IllegalArgumentException e) {
// log supported units, fall back or rethrow with user-facing message
throw new ApiException(400, "Unsupported time unit. Supported: seconds, minutes, hours, days, weeks, months, years");
} Prevention
- Maintain a canonical unit constant set and validate against it at input boundaries.
- Map common abbreviations to canonical forms before calling convertTime.
- Always use plural forms; never assume singular or abbreviated forms are accepted.
When it happens
Trigger: Calling convertTime(value, "minutes", "miliseconds") (typo), passing a singular form like "second" instead of "seconds", passing abbreviations like "min"/"hr", or passing a non-time unit like "celsius".
Common situations: Integrating with upstream systems that emit unit abbreviations or singular forms; copy-paste errors; locale-specific unit names; assuming singular forms are accepted.
Related errors
- inputUnit must be different from outputUnit.
- timeValue must be a non-negative number.
- Unit cannot be null.
- NULL_INPUT
- UNKNOWN_WORD
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b49afc1ac80b2641.
Report an issue: GitHub.