{"record":{"id":"09c4864b60303770","repo":"jwtk/jjwt","slug":"cannot-create-date-from-object-of-type-v-getclas","errorCode":null,"errorMessage":"Cannot create Date from object of type ${v.getClass().getName()}.","messagePattern":"Cannot create Date from object of type (.+?)\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/lang/JwtDateConverter.java","lineNumber":91,"sourceCode":"     * @param v the object value to represent as a Date.\n     * @return a {@link Date} equivalent of the specified object value using heuristics.\n     */\n    public static Date toDate(Object v) {\n        if (v == null) {\n            return null;\n        } else if (v instanceof Date) {\n            return (Date) v;\n        } else if (v instanceof Calendar) { //since 0.10.0\n            return ((Calendar) v).getTime();\n        } else if (v instanceof Number) {\n            //assume millis:\n            long millis = ((Number) v).longValue();\n            return new Date(millis);\n        } else if (v instanceof String) {\n            return parseIso8601Date((String) v); //ISO-8601 parsing since 0.10.0\n        } else {\n            String msg = \"Cannot create Date from object of type \" + v.getClass().getName() + \".\";\n            throw new IllegalArgumentException(msg);\n        }\n    }\n\n    /**\n     * Parses the specified ISO-8601-formatted string and returns the corresponding {@link Date} instance.\n     *\n     * @param value an ISO-8601-formatted string.\n     * @return a {@link Date} instance reflecting the specified ISO-8601-formatted string.\n     * @since 0.10.0\n     */\n    private static Date parseIso8601Date(String value) throws IllegalArgumentException {\n        try {\n            return DateFormats.parseIso8601Date(value);\n        } catch (ParseException e) {\n            String msg = \"String value is not a JWT NumericDate, nor is it ISO-8601-formatted. \" +\n                    \"All heuristics exhausted. Cause: \" + e.getMessage();\n            throw new IllegalArgumentException(msg, e);\n        }","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/lang/JwtDateConverter.java#L73-L109","documentation":"JwtDateConverter.toDate converts a JWT date claim value (exp, nbf, iat) into a java.util.Date. It accepts Number (epoch seconds/millis) and ISO-8601 String inputs; any other type throws IllegalArgumentException 'Cannot create Date from object of type ...'.","triggerScenarios":"A date claim deserialized as something other than Number or String - e.g. a Map/List from malformed JSON, a Boolean, or a custom object set programmatically into a claim typed as a date.","commonSituations":"Custom JSON deserializers producing non-standard types; manually building claims with wrong types (new Object()); third-party token issuers emitting dates in exotic formats parsed into containers.","solutions":["Pass epoch-millis Number (new Date().getTime()) or an ISO-8601 String for date claims.","Inspect the raw JSON of the token to see the claim's actual type and fix the producer.","Pre-validate with instanceof Number/String before passing to the converter, converting or rejecting other types."],"exampleFix":"// before\njwt.claim(\"exp\", someCustomDateObject); // unsupported type\n// after\njwt.claim(\"exp\", System.currentTimeMillis() / 1000L); // epoch seconds as Number","handlingStrategy":"type-guard","validationCode":"if (!(v instanceof Number) && !(v instanceof String)) {\n    throw new IllegalArgumentException(\"Date claim must be epoch number or ISO-8601 string: \" + v);\n}","typeGuard":"static Date toDate(Object v) {\n    if (v instanceof Number) return new Date(((Number) v).longValue());\n    if (v instanceof String) return Date.from(Instant.parse((String) v));\n    return null;\n}","tryCatchPattern":"try {\n    Date d = (Date) claims.get(\"exp\");\n} catch (IllegalArgumentException | ClassCastException e) {\n    // parse claim manually or reject the token\n}","preventionTips":["Always write date claims as epoch seconds/millis Numbers or ISO-8601 Strings","Validate third-party tokens' date claim types before processing","Use io.jsonwebtoken's Claims date getters (getExpiration) which handle conversion for you"],"tags":["date","type-mismatch","jwt-claims"],"backgroundTag":"invalid-date-format","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}