{"record":{"id":"dfdc8c8ad0129fc4","repo":"chinabugotech/hutool","slug":"this-is-not-a-mutable-object","errorCode":null,"errorMessage":"This is not a mutable object !","messagePattern":"This is not a mutable object !","errorType":"exception","errorClass":"DateException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/date/DateTime.java","lineNumber":432,"sourceCode":"\t * @return this\n\t */\n\tpublic DateTime setField(int field, int value) {\n\t\tfinal Calendar calendar = toCalendar();\n\t\tcalendar.set(field, value);\n\n\t\tDateTime dt = this;\n\t\tif (false == mutable) {\n\t\t\tdt = ObjectUtil.clone(this);\n\t\t}\n\t\treturn dt.setTimeInternal(calendar.getTimeInMillis());\n\t}\n\n\t@Override\n\tpublic void setTime(long time) {\n\t\tif (mutable) {\n\t\t\tsuper.setTime(time);\n\t\t} else {\n\t\t\tthrow new DateException(\"This is not a mutable object !\");\n\t\t}\n\t}\n\n\t/**\n\t * 获得年的部分\n\t *\n\t * @return 年的部分\n\t */\n\tpublic int year() {\n\t\treturn getField(DateField.YEAR);\n\t}\n\n\t/**\n\t * 获得当前日期所属季度，从1开始计数<br>\n\t *\n\t * @return 第几个季度 {@link Quarter}\n\t */\n\tpublic int quarter() {","sourceCodeStart":414,"sourceCodeEnd":450,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/date/DateTime.java#L414-L450","documentation":"Thrown by DateTime.setTime(long) when the DateTime is configured as immutable (mutable == false). By default DateTime is mutable, but calling setMutable(false) or creating an immutable instance makes setTime() a prohibited mutation. The design intent is that immutable DateTime instances must not be altered in place — use offset(), setField(), or other methods that return new instances.","triggerScenarios":"Creating DateTime with mutable=false (via setMutable(false)) then calling setTime(millis). Passing an immutable DateTime to legacy code or third-party libraries that call setTime() on Date objects (since DateTime extends Date). Deserializing into an immutable DateTime via a framework that uses setTime().","commonSituations":"Sharing DateTime instances across threads and setting mutable=false for thread safety, then calling setTime() inadvertently. Date-oriented APIs that call setTime() on any java.util.Date subclass. Confusion between offset()/setField() (which handle immutability by cloning) and setTime() (which does not).","solutions":["If you need to change the time, use offset() or setField() which respect immutability and return new DateTime instances.","If mutation is required, ensure mutable is true: dateTime.setMutable(true) before calling setTime().","Create a new DateTime with the desired time: new DateTime(millis) instead of setTime() on an existing immutable instance.","When passing DateTime to APIs that call setTime(), wrap it in a plain java.util.Date or ensure it is mutable."],"exampleFix":"// before\nDateTime dt = DateUtil.date();\ndt.setMutable(false);\ndt.setTime(newMillis); // throws DateException\n\n// after\nDateTime dt = DateUtil.date();\ndt.setMutable(false);\nDateTime updated = new DateTime(newMillis);\n// or: dt.setMutable(true); dt.setTime(newMillis);","handlingStrategy":"validation","validationCode":"if (!dateTime.isMutable()) {\n    // use new DateTime or set mutable first\n    return new DateTime(newMillis);\n}\ndateTime.setTime(newMillis);","typeGuard":"static boolean isMutable(DateTime dt) {\n    // check the mutable flag before calling setTime\n    return FieldUtil.getFieldValue(dt, \"mutable\") == Boolean.TRUE;\n}","tryCatchPattern":"try {\n    dateTime.setTime(millis);\n} catch (DateException e) {\n    // create a new instance instead\n    return new DateTime(millis);\n}","preventionTips":["Do not call setTime() on immutable DateTime instances; use offset(), setField(), or new DateTime().","If mutation is needed, call setMutable(true) first.","When passing DateTime to APIs that call setTime(), ensure it is mutable or wrap in a plain Date."],"tags":["date","datetime","immutable","mutable","set-time","date-exception"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}