chinabugotech/hutool · error · IllegalArgumentException

The date must not be null

Error message

The date must not be null

What it means

Thrown by CalendarUtil.isSameDay(Calendar, Calendar) when either cal1 or cal2 is null. The method requires two non-null Calendar instances to compare DAY_OF_YEAR, YEAR, and ERA fields. It does not treat null as 'unknown' — it is a hard precondition failure.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java:372

	 * 修改某年的结束时间
	 *
	 * @param calendar 日期 {@link Calendar}
	 * @return {@link Calendar}
	 */
	public static Calendar endOfYear(Calendar calendar) {
		return ceiling(calendar, DateField.YEAR);
	}

	/**
	 * 比较两个日期是否为同一天
	 *
	 * @param cal1 日期1
	 * @param cal2 日期2
	 * @return 是否为同一天
	 */
	public static boolean isSameDay(Calendar cal1, Calendar cal2) {
		if (cal1 == null || cal2 == null) {
			throw new IllegalArgumentException("The date must not be null");
		}

		if(ObjUtil.notEqual(cal1.getTimeZone(), cal2.getTimeZone())){
			// 统一时区
			cal2 = changeTimeZone(cal2, cal1.getTimeZone());
		}

		return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && //
			cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && //
			cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA);
	}

	/**
	 * 是否为本月最后一天
	 *
	 * @param calendar {@link Calendar}
	 * @return 是否为本月最后一天
	 * @since 5.8.27

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Null-check both Calendar arguments before calling isSameDay: if (cal1 != null && cal2 != null) { ... }.
  2. Default null calendars to a sentinel or 'now' with ObjectUtil.defaultIfNull(cal, Calendar.getInstance()) if business logic permits.
  3. Use DateUtil.isSameDay(Date, Date) with Date objects if your inputs are dates, but still guard for null.
  4. Wrap the call site in a utility method that returns false or Optional for null inputs.

Example fix

// before
boolean same = CalendarUtil.isSameDay(cal1, cal2); // throws if null

// after
boolean same = cal1 != null && cal2 != null && CalendarUtil.isSameDay(cal1, cal2);
Defensive patterns

Strategy: validation

Validate before calling

if (cal1 == null || cal2 == null) {
    return false; // or throw your own domain exception
}
CalendarUtil.isSameDay(cal1, cal2);

Type guard

static boolean isComparable(Calendar... cals) {
    for (Calendar c : cals) { if (c == null) return false; }
    return true;
}

Prevention

When it happens

Trigger: Calling CalendarUtil.isSameDay(null, cal2) or isSameDay(cal1, null). Passing a Calendar obtained from a nullable source (e.g., a date field from a partially populated DTO or database row) without a null check.

Common situations: Comparing dates from optional database columns that can be null. Comparing user-supplied dates in a form where the user left a date field blank. Chain calls where an upstream method returns null Calendar on parse failure.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/ad6af5317cabf71f. Report an issue: GitHub.