DateTimeFormatter Case Insensitive Parse
Accept month names in any letter case by building a DateTimeFormatter with parseCaseInsensitive instead of upper-casing the input yourself first.
Code
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.Locale;
public class MonthNameCase {
public static void main(String[] args) {
DateTimeFormatter strict = DateTimeFormatter.ofPattern("uuuu-MMM-dd", Locale.ENGLISH);
try {
LocalDate.parse("2026-jan-07", strict);
System.out.println("strict accepted lowercase: unexpected");
} catch (DateTimeParseException rejected) {
System.out.println("strict rejected lowercase month");
}
DateTimeFormatter lenient = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("uuuu-MMM-dd")
.toFormatter(Locale.ENGLISH);
System.out.println("jan -> " + LocalDate.parse("2026-jan-07", lenient));
System.out.println("JAN -> " + LocalDate.parse("2026-JAN-07", lenient));
System.out.println("Jan -> " + LocalDate.parse("2026-Jan-07", lenient));
}
}Following the execution — DateTimeFormatter Case Insensitive Parse
The code’s whole job belongs to the class MonthNameCase and the function main.
Watch out for
- Each symbol the file uses is a language built-in, so nothing gets fetched.
Using it
Signature: public static void main(String[] args)
The file’s steps show up in the output as strict accepted lowercase: unexpected, strict rejected lowercase month, jan ->.
Key statements
DateTimeFormatter strict = DateTimeFormatter.ofPattern("uuuu-MMM-dd", Locale.ENGLISH);
DateTimeFormatter lenient = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("uuuu-MMM-dd")
.toFormatter(Locale.ENGLISH);
LocalDate.parse("2026-jan-07", strict);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Case ConverterConvert text between lowercase, uppercase, title, sentence, camelCase, snake_case, and kebab-case while preserving Unicode letters and numbers.
FAQ
- What does DateTimeFormatter Case Insensitive Parse print when run?
- The script runs standalone; its 5 labeled outputs include `strict accepted lowercase: unexpected`, `strict rejected lowercase month`, `jan ->`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.