Japanese Imperial Era Year (Reiwa/Heisei/Showa) Regex for Java
/^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching japanese imperial era year (reiwa/heisei/showa), ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Japanese Imperial Era Year (Reiwa/Heisei/Showa)
// ReDoS-safe | RegexVault — Localization > Date Formats
import java.util.regex.Pattern;
public class JapaneseImperialEraYearReiwaheiseishowaValidator {
private static final Pattern PATTERN =
Pattern.compile("^(?:(令和|R)((?:[1-9]|[1-9][0-9]))|(平成|H)((?:[1-9]|[12][0-9]|3[01]))|(昭和|S)((?:[1-9]|[12][0-9]|3[0-9]|6[0-4])))年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("令和6年1月15日")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
令和6年1月15日 | 令和0年1月1日 |
R6年3月31日 | R100年1月1日 |
平成31年4月30日 | 明治30年1月1日 |
H31年1月1日 | 2024年1月15日 |
昭和64年1月7日 | — |
When to use this pattern
This pattern is drawn from the Localization > Date Formats category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Era year 1 of the new era and the last year of the previous era overlap — both are valid for the year of transition. 平成31年 and 令和元年 are both 2019.
Technical Notes
Era limits: 令和/Reiwa started May 1, 2019 (max year currently ~6). 平成/Heisei: 1-31 (1989-2019). 昭和/Showa: 1-64 (1926-1989). Meiji and Taisho are pre-1926 and excluded here. Convert to Gregorian for storage.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern