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.9View on GitHub (pinned to 8870454b2a)
Solutions
- Exclude DateField.ERA from any field selection logic: if (datePart != DateField.ERA) { dateTime.offset(datePart, offset); }.
- Validate user-supplied field names against a whitelist of supported fields (exclude ERA).
- If era manipulation is truly needed, work with Calendar directly via set(Calendar.ERA, ...) instead of offset.
- 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
- Filter DateField.ERA from any field selection logic before calling offset().
- Validate user-supplied field names against a supported whitelist.
- When iterating DateField.values(), skip ERA.
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
- The date must not be null
- Date and Patterns must not be null
- This is not a mutable object !
- Date to compare is null !
- Parse [{}] with format [{}] error!
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/23b441f2607d3e0f.
Report an issue: GitHub.