{"record":{"id":"adcf88ec5cdbb120","repo":"TheAlgorithms/Java","slug":"timevalue-must-be-a-non-negative-number","errorCode":null,"errorMessage":"timeValue must be a non-negative number.","messagePattern":"timeValue must be a non-negative number\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/TimeConverter.java","lineNumber":75,"sourceCode":"        }\n    }\n\n    private static final Map<String, TimeUnit> UNIT_LOOKUP\n        = 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));\n\n    /**\n     * Converts a time value from one unit to another.\n     *\n     * @param timeValue the numeric value of time to convert; must be non-negative\n     * @param unitFrom the unit of the input value (e.g., \"minutes\", \"hours\")\n     * @param unitTo the unit to convert into (e.g., \"seconds\", \"days\")\n     * @return the converted value in the target unit, rounded to three decimals\n     * @throws IllegalArgumentException if {@code timeValue} is negative\n     * @throws IllegalArgumentException if either {@code unitFrom} or {@code unitTo} is not supported\n     */\n    public static double convertTime(double timeValue, String unitFrom, String unitTo) {\n        if (timeValue < 0) {\n            throw new IllegalArgumentException(\"timeValue must be a non-negative number.\");\n        }\n\n        TimeUnit from = resolveUnit(unitFrom);\n        TimeUnit to = resolveUnit(unitTo);\n\n        double secondsValue = from.toSeconds(timeValue);\n        double converted = to.fromSeconds(secondsValue);\n\n        return Math.round(converted * 1000.0) / 1000.0;\n    }\n\n    private static TimeUnit resolveUnit(String unit) {\n        if (unit == null) {\n            throw new IllegalArgumentException(\"Unit cannot be null.\");\n        }\n        TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));\n        if (resolved == null) {\n            throw new IllegalArgumentException(\"Invalid unit '\" + unit + \"'. Supported units are: \" + UNIT_LOOKUP.keySet());","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/TimeConverter.java#L57-L93","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check that timeValue >= 0 before calling convertTime.","If negative values indicate an error condition, log and reject them at the boundary.","If negative values are meaningful in your domain (e.g., time before epoch), use Math.abs or handle the sign separately."],"exampleFix":"// before\ndouble result = TimeConverter.convertTime(timeValue, \"minutes\", \"seconds\");\n\n// after\nif (timeValue < 0) {\n    throw new IllegalArgumentException(\"timeValue must be non-negative, got: \" + timeValue);\n}\ndouble result = TimeConverter.convertTime(timeValue, \"minutes\", \"seconds\");","handlingStrategy":"validation","validationCode":"if (timeValue < 0) {\n    throw new IllegalArgumentException(\"timeValue must be non-negative, got: \" + timeValue);\n}\ndouble result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);","typeGuard":"static boolean isNonNegativeTime(double value) {\n    return value >= 0;\n}","tryCatchPattern":"try {\n    double result = TimeConverter.convertTime(timeValue, unitFrom, unitTo);\n} catch (IllegalArgumentException e) {\n    // negative time value or invalid unit; log and handle\n    logger.warn(\"Invalid time conversion input: value={}, from={}, to={}\", timeValue, unitFrom, unitTo);\n}","preventionTips":["Check timeValue >= 0 before calling convertTime.","If negative values represent a valid state (e.g., time before epoch), use Math.abs or handle the sign.","Validate at the computation boundary where the value's sign is determined."],"tags":["time-conversion","input-validation","range-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}