Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Date and Time

The DateTimeFormatter class

Reference for java.time.format.DateTimeFormatter: ISO constants, pattern letters, locale-aware styles, formatting, strict parsing and the usual pattern traps.

DateTimeFormatter converts date-time objects to text and text back to objects. It is immutable and thread-safe, so you can keep one in a static final field and share it freely. That is a big difference from the old SimpleDateFormat.

There are three ways to get a formatter: predefined ISO constants such as ISO_LOCAL_DATE, locale-aware styles through ofLocalizedDate(FormatStyle), and your own pattern with ofPattern("..."). Patterns are case-sensitive: MM is the month, mm is the minute.

Java
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.US);
String text = LocalDate.of(2026, 3, 9).format(f);   // "09 Mar 2026"
LocalDate back = LocalDate.parse("09 Mar 2026", f);

Try it

Java Formatting with patterns and styles

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Parsing and its errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static DateTimeFormatter ofPattern(String pattern)
static DateTimeFormatter ofPattern(String pattern, Locale locale)
static DateTimeFormatter ofLocalizedDate(FormatStyle style)
String format(TemporalAccessor temporal)
TemporalAccessor parse(CharSequence text)
Member Description
ofPattern(p) Builds a formatter from pattern letters; uses the default locale.
withLocale(l) Returns a copy with another locale (names of months, weekdays, AM/PM).
withZone(z) Returns a copy that formats an Instant in the zone z.
withResolverStyle(s) STRICT, SMART (default) or LENIENT.
format(temporal) Returns the text; also temporal.format(formatter).
parse(text) Returns raw parsed fields; usually prefer LocalDate.parse(text, f).
ISO_LOCAL_DATE, ISO_LOCAL_DATE_TIME, ISO_INSTANT, ISO_OFFSET_DATE_TIME, RFC_1123_DATE_TIME Predefined constants.

Common pattern letters:

Letter Meaning Example
y / u Year of era / year yyyy gives 2026
M Month: M=3, MM=03, MMM=Mar, MMMM=March
d Day of month dd gives 09
E Day of week: EEE=Mon, EEEE=Monday
H / h Hour of day (0-23) / clock hour (1-12)
m, s, S Minute, second, fraction of second
a AM or PM
z, x, Z Zone name, offset (+01), offset (+0100)

Throws: DateTimeException from format if a field the pattern needs is missing (for example a zone letter on a LocalDate); DateTimeParseException from the parse methods.

Usage notes

  • Share formatters. They are immutable and thread-safe; build once, reuse everywhere. This is the main improvement over SimpleDateFormat.
  • yyyy versus uuuu. yyyy is year-of-era and only works reliably with strict parsing if you also supply an era. In strict mode use uuuu. For everyday formatting both look the same.
  • MM versus mm, HH versus hh. Uppercase M is month, lowercase m is minute. HH is 24-hour clock; hh needs a to be unambiguous.
  • Fix the locale for anything user-visible or compared in tests. Without it, month and weekday names depend on the machine's default locale.
  • Field must exist. Formatting a LocalDate with HH throws UnsupportedTemporalTypeException (a DateTimeException). Use a LocalDateTime or a matching pattern.
  • Parsing into the right type. Use LocalDate.parse(text, f) and similar. The default SMART resolver clamps an impossible day such as 31 April to the last valid day (30 April); use ResolverStyle.STRICT (with uuuu, not yyyy) to reject it.
  • Escape letters. Wrap literal text in single quotes; use two single quotes for a quote character. Unquoted letters from a to z that are not pattern letters throw IllegalArgumentException.
  • Instants need a zone. DateTimeFormatter.ofPattern("HH:mm").format(instant) fails unless you call withZone(...) first.