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.

JavaForm Validation

Code

snippet
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

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 ->`.