chinabugotech/hutool · error · IllegalArgumentException

ChronoUnit cannot be converted to TimeUnit: {}

Error message

ChronoUnit cannot be converted to TimeUnit: {}

What it means

Error "ChronoUnit cannot be converted to TimeUnit: {}" thrown in chinabugotech/hutool.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/date/TemporalUtil.java:105

			return null;
		}
		switch (unit) {
			case NANOS:
				return TimeUnit.NANOSECONDS;
			case MICROS:
				return TimeUnit.MICROSECONDS;
			case MILLIS:
				return TimeUnit.MILLISECONDS;
			case SECONDS:
				return TimeUnit.SECONDS;
			case MINUTES:
				return TimeUnit.MINUTES;
			case HOURS:
				return TimeUnit.HOURS;
			case DAYS:
				return TimeUnit.DAYS;
			default:
				throw new IllegalArgumentException("ChronoUnit cannot be converted to TimeUnit: " + unit);
		}
	}

	/**
	 * 日期偏移,根据field不同加不同值(偏移会修改传入的对象)
	 *
	 * @param <T>    日期类型,如LocalDate或LocalDateTime
	 * @param time   {@link Temporal}
	 * @param number 偏移量,正数为向后偏移,负数为向前偏移
	 * @param field  偏移单位,见{@link ChronoUnit},不能为null
	 * @return 偏移后的日期时间
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Temporal> T offset(T time, long number, TemporalUnit field) {
		if (null == time) {
			return null;
		}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Only convert ChronoUnits that map to TimeUnit (NANOS, MICROS, MILLIS, SECONDS, MINUTES, HOURS, DAYS).
  2. Handle larger units (WEEKS, MONTHS, YEARS) separately, e.g. via Duration or Period.

When it happens

Trigger: Thrown at hutool-core/src/main/java/cn/hutool/core/date/TemporalUtil.java:105 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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