{"record":{"id":"23b441f2607d3e0f","repo":"chinabugotech/hutool","slug":"era-is-not-support-offset","errorCode":null,"errorMessage":"ERA is not support offset!","messagePattern":"ERA is not support offset!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/date/DateTime.java","lineNumber":343,"sourceCode":"\tpublic DateTime(CharSequence dateStr, DateParser dateParser, boolean lenient) {\n\t\tthis(parse(dateStr, dateParser, lenient));\n\t}\n\n\t// -------------------------------------------------------------------- Constructor end\n\n\t// -------------------------------------------------------------------- offset start\n\n\t/**\n\t * 调整日期和时间<br>\n\t * 如果此对象为可变对象，返回自身，否则返回新对象，设置是否可变对象见{@link #setMutable(boolean)}\n\t *\n\t * @param datePart 调整的部分 {@link DateField}\n\t * @param offset   偏移量，正数为向后偏移，负数为向前偏移\n\t * @return 如果此对象为可变对象，返回自身，否则返回新对象\n\t */\n\tpublic DateTime offset(DateField datePart, int offset) {\n\t\tif (DateField.ERA == datePart) {\n\t\t\tthrow new IllegalArgumentException(\"ERA is not support offset!\");\n\t\t}\n\n\t\tfinal Calendar cal = toCalendar();\n\t\t//noinspection MagicConstant\n\t\tcal.add(datePart.getValue(), offset);\n\n\t\tDateTime dt = mutable ? this : ObjectUtil.clone(this);\n\t\treturn dt.setTimeInternal(cal.getTimeInMillis());\n\t}\n\n\t/**\n\t * 调整日期和时间<br>\n\t * 返回调整后的新DateTime，不影响原对象\n\t *\n\t * @param datePart 调整的部分 {@link DateField}\n\t * @param offset   偏移量，正数为向后偏移，负数为向前偏移\n\t * @return 如果此对象为可变对象，返回自身，否则返回新对象\n\t * @since 3.0.9","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java#L325-L361","documentation":"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().","triggerScenarios":"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().","commonSituations":"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.","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."],"exampleFix":"// before\ndateTime.offset(DateField.ERA, 1); // throws\n\n// after\nif (datePart != DateField.ERA) {\n    dateTime.offset(datePart, offset);\n}","handlingStrategy":"validation","validationCode":"if (datePart == DateField.ERA) {\n    throw new IllegalArgumentException(\"ERA offset is not supported\");\n}\ndateTime.offset(datePart, offset);","typeGuard":"static boolean isOffsettable(DateField field) {\n    return field != DateField.ERA;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["date","datetime","offset","era","argument-validation"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}