TheAlgorithms/Java · error · IllegalArgumentException

timeValue must be a non-negative number.

Error message

timeValue must be a non-negative number.

What it means

Thrown by TimeConverter.convertTime when the timeValue argument is negative. Time durations are expected to be non-negative; a negative value has no physical meaning in the conversion context and could produce confusing results in the seconds-based conversion chain (from.toSeconds / to.fromSeconds).

Source

Thrown at src/main/java/com/thealgorithms/conversions/TimeConverter.java:75

        }
    }

    private static final Map<String, TimeUnit> UNIT_LOOKUP
        = Map.ofEntries(Map.entry("seconds", TimeUnit.SECONDS), Map.entry("minutes", TimeUnit.MINUTES), Map.entry("hours", TimeUnit.HOURS), Map.entry("days", TimeUnit.DAYS), Map.entry("weeks", TimeUnit.WEEKS), Map.entry("months", TimeUnit.MONTHS), Map.entry("years", TimeUnit.YEARS));

    /**
     * Converts a time value from one unit to another.
     *
     * @param timeValue the numeric value of time to convert; must be non-negative
     * @param unitFrom the unit of the input value (e.g., "minutes", "hours")
     * @param unitTo the unit to convert into (e.g., "seconds", "days")
     * @return the converted value in the target unit, rounded to three decimals
     * @throws IllegalArgumentException if {@code timeValue} is negative
     * @throws IllegalArgumentException if either {@code unitFrom} or {@code unitTo} is not supported
     */
    public static double convertTime(double timeValue, String unitFrom, String unitTo) {
        if (timeValue < 0) {
            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());

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check that timeValue >= 0 before calling convertTime.
  2. If negative values indicate an error condition, log and reject them at the boundary.
  3. If negative values are meaningful in your domain (e.g., time before epoch), use Math.abs or handle the sign separately.

Example fix

// before
double result = TimeConverter.convertTime(timeValue, "minutes", "seconds");

// after
if (timeValue < 0) {
    throw new IllegalArgumentException("timeValue must be non-negative, got: " + timeValue);
}
double result = TimeConverter.convertTime(timeValue, "minutes", "seconds");
Defensive patterns

Strategy: validation

Validate before calling

if (timeValue < 0) {
    throw new IllegalArgumentException("timeValue must be non-negative, got: " + timeValue);
}
double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);

Type guard

static boolean isNonNegativeTime(double value) {
    return value >= 0;
}

Try / catch

try {
    double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);
} catch (IllegalArgumentException e) {
    // negative time value or invalid unit; log and handle
    logger.warn("Invalid time conversion input: value={}, from={}, to={}", timeValue, unitFrom, unitTo);
}

Prevention

When it happens

Trigger: Calling convertTime with a negative double value. Passing a value computed from a subtraction that can go negative. Supplying a value from an external source (sensor, API, database) that returned a negative reading.

Common situations: A timer or stopwatch computes elapsed time that wraps around or is set incorrectly. A data feed returns a negative duration due to a synchronization error. A database column stores a signed value where unsigned was expected.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/adcf88ec5cdbb120. Report an issue: GitHub.