Lesson · Date & Time API
Formatting and parsing: patterns, locales and strictness
14 min readFormatting & Parsing
DateTimeFormatter turns values into text and text into values. ISO 8601 is the default both ways; custom patterns and locales handle what humans read, and strict parsing turns bad input into an exception instead of a guess.
Start with ISO: free round-trips
Every java.time type parses and prints ISO 8601 out of the box through its own parse and toString. 2026-09-21T14:30:05+02:00[Europe/Berlin] is both the debug output and a valid wire format, so the fastest correct choice for machine-facing text is no formatter at all.
DateTimeFormatter.ISO_LOCAL_DATE and friends name those built-ins explicitly for code that wants to be obvious about it. Log lines, JSON payloads and URLs should stay on these: they sort lexicographically, survive every locale, and cost nothing to parse.
LocalDate.parse("2026-09-21"); // built in dateTime.toString(); // 2026-09-21T14:30:05: also parseable ZonedDateTime.parse("2026-09-21T14:30:05+02:00[Europe/Berlin]"); // full round trip
Pattern letters, and the pairs that bite
ofPattern speaks the classic letter language: y year, M month, d day, H hour (0-23), h hour (1-12), m minute, s second, E weekday, a AM/PM. Case is significant, which produces the two classic bugs: mm is minutes where MM is months, and YYYY is the week-based year where yyyy is the calendar year.
Week-based year means a date in early January can belong to the previous year's last week, so formatting 2026-01-01 with YYYY can print 2025. Use yyyy for calendars, YYYY only for week-numbering schemes that genuinely need it.
| Letter | Means | 2026-09-21T14:05:30 shows |
|---|---|---|
| yyyy / YYYY | calendar year / week-based year | 2026 / 2026, but they diverge near New Year |
| MM / MMM / MMMM | month number / short name / full name | 09 / Sep / September |
| dd / d | day with 2 digits / day | 21 / 21 |
| HH / hh | hour 0-23 / hour 1-12 with a | 14 / 02 |
| mm / ss | minute / second | 05 / 30 |
| EEEE | weekday name | Monday (or Montag in German) |
Pattern builder · 2026-09-21T14:05:30
letters
separators
ofPattern("yyyy-MM-dd HH:mm", Locale.ROOT)
renders as
2026-09-21 14:05
Parsing: fail loudly, fail early
parse throws DateTimeParseException on anything that does not fit, and the message names the exact problem: which field, which value, which valid range. That is the behavior you want at the boundary; nothing is guessed, nothing is half-parsed.
By default the modern formatters parse strictly: no lenient rollovers. 2026-13-40 fails instead of becoming 2027-02-09, which is the difference between a rejected request and a silent data corruption.
LocalDate.parse("2026-13-40", DateTimeFormatter.ISO_LOCAL_DATE); // DateTimeParseException: Text '2026-13-40' could not be parsed: // Invalid value for MonthOfYear (valid values 1 - 12): 13
Locales decide how it reads, not what it means
Month and weekday names are locale data, so a formatter with EEEE and MMMM produces English defaults or the Locale you pass. Parsing with a locale-aware formatter accepts that locale's names: a German date reads Montag, 21. September 2026, and parsing English names fails unless the locale says otherwise.
Always pass Locale.ROOT or an explicit locale for machine-facing formats; the default locale of the JVM is another hidden machine dependency, the same class of bug as the default time zone.
DateTimeFormatter german = DateTimeFormatter.ofPattern("EEEE, d. MMMM yyyy", Locale.GERMANY); LocalDate.of(2026, 9, 21).format(german); // Montag, 21. September 2026
The short list of real-world traps
Nearly every formatting bug in production is one of these five. They are worth memorizing as a set:
- mm versus MM: minutes and months differ by case only; a formatter with the wrong one compiles and lies.
- YYYY versus yyyy: week-based versus calendar year; they disagree around New Year's Eve.
- Default locale: ofPattern(pattern) uses Locale.getDefault(); pin the locale when the output is consumed by anything but a human.
- Lenient assumptions: java.time parses strictly; if older lenient behavior is required, that must be a deliberate, tested choice.
- DateTimeFormatter as a constant: create once, keep it in a static final, share it; it is immutable and thread-safe.
A formatter is a contract between your code and the outside world. Keep it in one place per format, named after the format, tested in both directions with a sample value.
Pattern letters are a language
Formatting code fails silently in ways configuration cannot: mm where MM belongs does not throw, it prints minutes where a month should be. Learn the letters the way you learned printf verbs, and pin the locale like you pin the time zone.
Interview angles
What does DateTimeFormatter add over SimpleDateFormat?+
Immutability and thread safety, strict parsing by default, locale-aware names, and built-in ISO round-trips. The old formatter keeps mutable state while working, so sharing one instance between threads corrupts output.
Why is MM not the same as mm?+
Pattern letters are case-sensitive: MM is the month, mm is the minute. Mixing them compiles fine and formats the wrong field, so it is a silent bug that tests catch only if they assert exact output.
What is wrong with YYYY?+
YYYY is the week-based year (ISO week numbering), not the calendar year. For 2026-01-01 it may print 2025 because the week belongs to the previous year. Use yyyy unless you specifically need week-year semantics.
How do you parse a date strictly?+
Use DateTimeFormatter (strict by default) and parse; invalid input throws DateTimeParseException naming the offending field and valid range. Avoid reintroducing lenient behavior unless it is deliberate and tested.
Should formatters be created per call?+
No. Compile them once into static final constants (they are immutable and thread-safe) and reuse. Per-call creation costs allocations and hides the format contract inside method bodies.