chinabugotech/hutool · error · IllegalArgumentException

ERA is not support offset!

Error message

ERA is not support offset!

What it means

Thrown by DateTime.offset(DateField, int) when datePart is DateField.ERA. The ERA field (BC vs AD) is explicitly excluded from offset operations because shifting era is semantically nonsensical for date arithmetic. All other DateField values are supported via Calendar.add().

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/date/DateTime.java:343

	public DateTime(CharSequence dateStr, DateParser dateParser, boolean lenient) {
		this(parse(dateStr, dateParser, lenient));
	}

	// -------------------------------------------------------------------- Constructor end

	// -------------------------------------------------------------------- offset start

	/**
	 * 调整日期和时间<br>
	 * 如果此对象为可变对象,返回自身,否则返回新对象,设置是否可变对象见{@link #setMutable(boolean)}
	 *
	 * @param datePart 调整的部分 {@link DateField}
	 * @param offset   偏移量,正数为向后偏移,负数为向前偏移
	 * @return 如果此对象为可变对象,返回自身,否则返回新对象
	 */
	public DateTime offset(DateField datePart, int offset) {
		if (DateField.ERA == datePart) {
			throw new IllegalArgumentException("ERA is not support offset!");
		}

		final Calendar cal = toCalendar();
		//noinspection MagicConstant
		cal.add(datePart.getValue(), offset);

		DateTime dt = mutable ? this : ObjectUtil.clone(this);
		return dt.setTimeInternal(cal.getTimeInMillis());
	}

	/**
	 * 调整日期和时间<br>
	 * 返回调整后的新DateTime,不影响原对象
	 *
	 * @param datePart 调整的部分 {@link DateField}
	 * @param offset   偏移量,正数为向后偏移,负数为向前偏移
	 * @return 如果此对象为可变对象,返回自身,否则返回新对象
	 * @since 3.0.9

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Exclude DateField.ERA from any field selection logic: if (datePart != DateField.ERA) { dateTime.offset(datePart, offset); }.
  2. Validate user-supplied field names against a whitelist of supported fields (exclude ERA).
  3. If era manipulation is truly needed, work with Calendar directly via set(Calendar.ERA, ...) instead of offset.
  4. Filter enum values: DateField.values() minus ERA when building selectable options.

Example fix

// before
dateTime.offset(DateField.ERA, 1); // throws

// after
if (datePart != DateField.ERA) {
    dateTime.offset(datePart, offset);
}
Defensive patterns

Strategy: validation

Validate before calling

if (datePart == DateField.ERA) {
    throw new IllegalArgumentException("ERA offset is not supported");
}
dateTime.offset(datePart, offset);

Type guard

static boolean isOffsettable(DateField field) {
    return field != DateField.ERA;
}

Prevention

When it happens

Trigger: Calling dateTime.offset(DateField.ERA, 1). Dynamically selecting a DateField from user input or config and passing ERA. Enum iteration that includes ERA feeding into offset().

Common situations: Generic date-manipulation code that iterates over DateField values. User-facing API that accepts a field name and maps it to DateField without filtering ERA. Test harness exercising all DateField enum values.

Related errors


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