Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Formatting and parsing dates

By default dates are printed in ISO format: 2026-10-04. That's convenient for programs, not for people — in the US people expect 10/04/2026, in much of Europe 04.10.2026. The DateTimeFormatter class turns a date into a string of the right shape and back.

A format pattern

A pattern describes what the date looks like: each letter stands for a part of it. DateTimeFormatter.ofPattern creates a formatter, and the date's format method applies it:

void main() {
    LocalDate date = LocalDate.of(2026, 10, 4);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
 
    IO.println(date.format(formatter));
}

Output:

04.10.2026

All characters that aren't pattern letters — dots, dashes, spaces — are printed as is.

Pattern letters

The most useful letters:

LetterMeaningExample
yyyyyear2026
MMmonth as a number10
MMMMmonth nameOctober
ddday of month04
EEEEday of weekSunday
HHhours, 0–2309
mmminutes05
ssseconds00

How many times a letter is repeated changes the look: M gives 10, MMM the short Oct, MMMM the full name.

void main() {
    LocalDateTime start = LocalDateTime.of(2026, 10, 4, 9, 5);
 
    IO.println(start.format(DateTimeFormatter.ofPattern("HH:mm")));
    IO.println(start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
}

Output:

09:05
2026-10-04 09:05:00

Names in a given language

Month and weekday names depend on the language. You set it with a Locale object as the second argument of ofPattern:

void main() {
    LocalDate date = LocalDate.of(2026, 10, 4);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy", Locale.ENGLISH);
 
    IO.println(date.format(formatter));
}

Output:

Sunday, October 4, 2026

If you don't specify the language, Java uses the operating system's language, and the output differs from computer to computer.

Parsing a string

The reverse operation is parsing: turning a string into a date. The LocalDate.parse method takes the string and the same formatter:

void main() {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
 
    LocalDate date = LocalDate.parse("25.12.2026", formatter);
 
    IO.println(date);
    IO.println(date.getDayOfWeek());
}

Output:

2026-12-25
FRIDAY

A string in ISO format, such as 2026-12-25, can be parsed without any formatter: LocalDate.parse("2026-12-25"). If a string doesn't fit the pattern, a DateTimeParseException is thrown.

Ready-made formats

For exchanging data between programs there are ready-made formatters such as DateTimeFormatter.ISO_LOCAL_DATE. For people there's ofLocalizedDate, which picks the date style customary in a country:

void main() {
    LocalDate date = LocalDate.of(2026, 10, 4);
 
    IO.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
    IO.println(date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(Locale.UK)));
    IO.println(date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(Locale.US)));
}

Output:

2026-10-04
4 October 2026
10/4/26

Common mistake

Pattern letters are case-sensitive: MM is the month and mm is minutes. A LocalDate has no minutes, so such a pattern causes an exception:

void main() {
    LocalDate date = LocalDate.of(2026, 10, 4);
 
    IO.println(date.format(DateTimeFormatter.ofPattern("dd.mm.yyyy")));
}

Output:

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: MinuteOfHour
	at Main.main(Main.java:4)

A similar trap is YYYY instead of yyyy: a capital Y means the "week-based year", which can differ from the ordinary year in the last days of December. Check your pattern against the table above.

Thanks for reading!

Donate to the site — we work hard for you.