{"record":{"id":"f8baf23bf2707e0f","repo":"chinabugotech/hutool","slug":"date-to-compare-is-null","errorCode":null,"errorMessage":"Date to compare is null !","messagePattern":"Date to compare is null !","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/date/DateTime.java","lineNumber":801,"sourceCode":"\t */\n\tpublic boolean isIn(Date beginDate, Date endDate) {\n\t\tlong beginMills = beginDate.getTime();\n\t\tlong endMills = endDate.getTime();\n\t\tlong thisMills = this.getTime();\n\n\t\treturn thisMills >= Math.min(beginMills, endMills) && thisMills <= Math.max(beginMills, endMills);\n\t}\n\n\t/**\n\t * 是否在给定日期之前\n\t *\n\t * @param date 日期\n\t * @return 是否在给定日期之前\n\t * @since 4.1.3\n\t */\n\tpublic boolean isBefore(Date date) {\n\t\tif (null == date) {\n\t\t\tthrow new NullPointerException(\"Date to compare is null !\");\n\t\t}\n\t\treturn compareTo(date) < 0;\n\t}\n\n\t/**\n\t * 是否在给定日期之前或与给定日期相等\n\t *\n\t * @param date 日期\n\t * @return 是否在给定日期之前或与给定日期相等\n\t * @since 3.0.9\n\t */\n\tpublic boolean isBeforeOrEquals(Date date) {\n\t\tif (null == date) {\n\t\t\tthrow new NullPointerException(\"Date to compare is null !\");\n\t\t}\n\t\treturn compareTo(date) <= 0;\n\t}\n","sourceCodeStart":783,"sourceCodeEnd":819,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java#L783-L819","documentation":"Thrown by DateTime.isBefore(Date) when the date argument is null. Unlike java.util.Date's natural behavior (which would NPE later), Hutool checks explicitly and throws NullPointerException with a descriptive message. The method delegates to compareTo(date), so a null argument is caught at the guard.","triggerScenarios":"Calling dateTime.isBefore(null). Passing a Date variable from a nullable source (optional DB column, absent JSON field) without null-checking.","commonSituations":"Comparing against dates from optional fields that may be null. Processing collections of dates where some elements are null. Chaining calls where a prior parse or lookup returned null.","solutions":["Null-check the argument: if (date != null) dateTime.isBefore(date).","Use Objects.requireNonNullElse(date, fallback) to provide a default.","Wrap in a null-safe comparison utility that returns false or Optional when the argument is null.","Validate upstream: ensure date sources never produce null."],"exampleFix":"// before\nboolean before = dateTime.isBefore(otherDate); // throws if null\n\n// after\nboolean before = otherDate != null && dateTime.isBefore(otherDate);","handlingStrategy":"validation","validationCode":"if (date == null) {\n    return false; // or handle per business rule\n}\ndateTime.isBefore(date);","typeGuard":"static boolean isNotNull(Date date) {\n    return date != null;\n}","tryCatchPattern":null,"preventionTips":["Always null-check the Date argument before calling isBefore().","Use a null-safe comparison utility that returns false for null.","Ensure date sources (DB, API, parsing) produce non-null values."],"tags":["date","datetime","null-check","comparison","null-pointer"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}