chinabugotech/hutool · error · IllegalArgumentException

Illegal pattern component: {}

Error message

Illegal pattern component: {}

What it means

Thrown by FastDatePrinter while tokenizing a pattern: a token (pattern letter) was encountered that has no case in the format-rule switch, so it falls to the default branch. Unlike the parser, this occurs when building the printer (formatter) for a pattern whose leading letter is unsupported.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/date/format/FastDatePrinter.java:202

				case 'Z': // time zone (value)
					if (tokenLen == 1) {
						rule = TimeZoneNumberRule.INSTANCE_NO_COLON;
					} else if (tokenLen == 2) {
						rule = Iso8601_Rule.ISO8601_HOURS_COLON_MINUTES;
					} else {
						rule = TimeZoneNumberRule.INSTANCE_COLON;
					}
					break;
				case '\'': // literal text
					final String sub = token.substring(1);
					if (sub.length() == 1) {
						rule = new CharacterLiteral(sub.charAt(0));
					} else {
						rule = new StringLiteral(sub);
					}
					break;
				default:
					throw new IllegalArgumentException("Illegal pattern component: " + token);
			}

			rules.add(rule);
		}

		return rules;
	}

	/**
	 * <p>
	 * Performs the parsing of tokens.
	 * </p>
	 *
	 * @param pattern  the pattern
	 * @param indexRef index references
	 * @return parsed token
	 */
	protected String parseToken(String pattern, int[] indexRef) {

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Inspect the reported token and replace it with a supported pattern letter.
  2. Wrap any literal alphabetic text in single quotes so it is treated as a literal, not a token.
  3. Cross-check the letter against FastDatePrinter's switch cases (y,M,d,H,m,s,S,E,u,D,F,w,W,a,k,K,X,z,Z).

Example fix

// before (unknown token)
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd 'at' T");
// after (escape literal letters, use real tokens)
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd 'at' HH:mm");
Defensive patterns

Strategy: validation

Validate before calling

void assertPrintable(String p){
  // quick guard: any unquoted alphabetic token must be a known printer letter
  Set<Character> ok = Set.of('y','M','d','H','k','K','h','m','s','S','E','u','D','F','w','W','a','G','z','Z','X');
  boolean inQuote=false;
  for(int i=0;i<p.length();i++){ char c=p.charAt(i); if(c=='\''){ inQuote=!inQuote; continue;} if(!inQuote && Character.isLetter(c) && !ok.contains(c)) throw new IllegalArgumentException("illegal token "+c); }
}

Try / catch

try { FastDateFormat.getInstance(pattern); }
catch (IllegalArgumentException e){ if(e.getMessage().startsWith("Illegal pattern component")) { /* fix token */ } else throw e; }

Prevention

When it happens

Trigger: Constructing a FastDateFormat/DateUtil.format pattern that begins with an unsupported pattern letter; stray characters that survive tokenization (e.g. a non-escaped letter). The offending token string is included.

Common situations: Patterns with letters Hutool's printer does not implement; copying patterns from other libraries; accidental letters that are not wrapped in quotes.

Related errors


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