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.2026All characters that aren't pattern letters — dots, dashes, spaces — are printed as is.
Pattern letters
The most useful letters:
| Letter | Meaning | Example |
|---|---|---|
yyyy | year | 2026 |
MM | month as a number | 10 |
MMMM | month name | October |
dd | day of month | 04 |
EEEE | day of week | Sunday |
HH | hours, 0–23 | 09 |
mm | minutes | 05 |
ss | seconds | 00 |
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:00Names 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, 2026If 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
FRIDAYA 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/26Common 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.