Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

DateTimeFormatter: patterns, locales, and strict parsing

Choose a pattern and locale to control the text, and a resolver style to control date validation. Learn why `MM` differs from `mm` and when to use `uuuu` instead of `yyyy`.

Version: Java SE 21

Syntax

java.time.format.DateTimeFormatter (Java SE 21)
static DateTimeFormatter ofPattern(String pattern)
static DateTimeFormatter ofPattern(String pattern, Locale locale)
DateTimeFormatter withLocale(Locale locale)
DateTimeFormatter withResolverStyle(ResolverStyle resolverStyle)

Locale: java.util.Locale
ResolverStyle: java.time.format.ResolverStyle

The static factory DateTimeFormatter.ofPattern builds a formatter from a pattern. The ofPattern(String) overload uses the default FORMAT locale; ofPattern(String, Locale) uses the supplied locale. Both start with SMART resolution. An invalid pattern throws IllegalArgumentException when the formatter is created.

Pattern letters are case-sensitive: dd is day of month, MM is numeric month, and mm is minute. The patterns dd-MM-uuuu, dd/MM/uuuu, and uuuu-MM-dd specify field order and separators. uuuu is the proleptic year (which includes year zero); yyyy is the year of era. In strict ISO parsing, yyyy with just a month and day is not enough to produce a LocalDate, because no era is assumed. Use uuuu for dates without an era.

DateTimeFormatter.withLocale sets the Locale on the returned formatter. It affects text such as full month names (MMMM) in both formatting and parsing, but does not reorder fields or replace separators in an explicit pattern. Formatters are immutable and thread-safe: keep the return value of withLocale or withResolverStyle, because the original is unchanged.

DateTimeFormatter.withResolverStyle controls the second phase of parsing: after the pattern has been used to read the fields, the formatter resolves them into a date. It does not change formatting. For ISO dates, STRICT rejects impossible dates, SMART can clamp a day in the range 1–31 to the end of the month, and LENIENT can carry excess days into the next month. LocalDate.parse(text, formatter) throws DateTimeParseException if the text does not match or cannot be resolved to a date.

Examples

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 1, 5);
        DateTimeFormatter english = DateTimeFormatter.ofPattern("dd MMMM uuuu", Locale.US);
        DateTimeFormatter french = english.withLocale(Locale.FRANCE);
        System.out.println(date.format(english));
        System.out.println(date.format(french));

        DateTimeFormatter smart = DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ROOT);
        DateTimeFormatter strict = smart.withResolverStyle(ResolverStyle.STRICT);
        String invalid = "31-02-2024";
        System.out.println("SMART: " + LocalDate.parse(invalid, smart));
        try {
            System.out.println("STRICT: " + LocalDate.parse(invalid, strict));
        } catch (DateTimeParseException e) {
            System.out.println("STRICT: rejected");
        }
    }
}

Result

05 January 2024
05 janvier 2024
SMART: 2024-02-29
STRICT: rejected

Changing the locale changes the month name, not the date layout. The names depend on the runtime's locale data. For `31-02-2024`, `SMART` returns `2024-02-29`, while `STRICT` rejects the input.

Related lessons

  • Java · Lesson 61 · Advanced · Version: 2

Sources