MyCATApache/Mycat-Server · error · IllegalArgumentException
Interval string does not match year-month format of 'y-m':
Error message
Interval string does not match year-month format of 'y-m':
What it means
fromYearMonthString parses a '[-]y-m' interval literal such as '3-6'. This IllegalArgumentException is thrown when the input string does not match the year-month regex (yearMonthPattern), e.g. missing the dash, non-numeric parts, or extra text.
Solutions
- Fix the input string to the strict '[-]y-m' format, e.g. '3-6' or '-1-2'
- Trim surrounding whitespace and remove quotes before parsing
- Validate the string against the format with a regex before calling the API
Example fix
// before
CalendarInterval iv = CalendarInterval.fromYearMonthString("3 years 6 months");
// after
CalendarInterval iv = CalendarInterval.fromYearMonthString("3-6"); Defensive patterns
Strategy: validation
Validate before calling
private static final Pattern Y_M = Pattern.compile("^-?\\d+-\\d{1,2}$");
static void requireYearMonth(String s) {
if (s == null || !Y_M.matcher(s.trim()).matches())
throw new IllegalArgumentException("expected 'y-m', got: " + s);
} Type guard
static boolean isYearMonthString(String s) {
return s != null && s.trim().matches("-?\\d+-\\d{1,2}");
} Try / catch
try {
iv = CalendarInterval.fromYearMonthString(s);
} catch (IllegalArgumentException e) {
log.error("Bad year-month interval '{}': {}", s, e.getMessage());
iv = CalendarInterval.ZERO;
} Prevention
- Always match the literal against ^-?\d+-\d{1,2}$ before parsing
- Trim and dequote user input first
- Never concatenate unit words like 'years' into the literal
When it happens
Trigger: Calling CalendarInterval.fromYearMonthString with a string that fails yearMonthPattern.matcher(s).matches(), e.g. fromYearMonthString("3 years 2 months"), fromYearMonthString("3:6"), fromYearMonthString("3-").
Common situations: SQL interval literals typed in the wrong format in a Mycat SQL query, config values copied from another database dialect, user-supplied input passed straight to the parser.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Interval string does not match day-time format of 'd…
- Interval string does not match second-nano format of…
- outside range [ , ]
- Interval year-month string was null
- Error parsing interval year-month string:
AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11).
Data as JSON: /api/errors/a98ec1401142eadb.
Report an issue: GitHub.
Appendix: source
Thrown at src/main/java/io/mycat/memory/unsafe/types/CalendarInterval.java:116
}
}
return result;
}
/**
* Parse YearMonth string in form: [-]YYYY-MM
*
* adapted from HiveIntervalYearMonth.valueOf
*/
public static CalendarInterval fromYearMonthString(String s) throws IllegalArgumentException {
CalendarInterval result = null;
if (s == null) {
throw new IllegalArgumentException("Interval year-month string was null");
}
s = s.trim();
Matcher m = yearMonthPattern.matcher(s);
if (!m.matches()) {
throw new IllegalArgumentException(
"Interval string does not match year-month format of 'y-m': " + s);
} else {
try {
int sign = m.group(1) != null && m.group(1).equals("-") ? -1 : 1;
int years = (int) toLongWithRange("year", m.group(2), 0, Integer.MAX_VALUE);
int months = (int) toLongWithRange("month", m.group(3), 0, 11);
result = new CalendarInterval(sign * (years * 12 + months), 0);
} catch (Exception e) {
throw new IllegalArgumentException(
"Error parsing interval year-month string: " + e.getMessage(), e);
}
}
return result;
}
/**
* Parse dayTime string in form: [-]d HH:mm:ss.nnnnnnnnn
*View on GitHub (pinned to 65f8d8beb7)