Java DateTimeFormatter Pattern Reference

Complete guide to Java's DateTimeFormatter patterns for the java.time API. Covers pattern letters, predefined formatters, locale-sensitive formatting, and migration from SimpleDateFormat.

Language-Specific

Detailed Explanation

Java DateTimeFormatter

Java 8 introduced the java.time package with DateTimeFormatter as the modern replacement for the legacy SimpleDateFormat. It is thread-safe, immutable, and supports the same pattern letter conventions.

Pattern Letters

Letter Meaning Example
yyyy Year (4-digit) 2026
yy Year (2-digit) 26
MM Month (zero-padded) 02
M Month (no padding) 2
MMM Abbreviated month Feb
MMMM Full month name February
dd Day (zero-padded) 28
d Day (no padding) 28
EEEE Full weekday Saturday
EEE Abbreviated weekday Sat
HH Hour 24h (zero-padded) 14
hh Hour 12h (zero-padded) 02
mm Minute 30
ss Second 00
SSS Millisecond 123
a AM/PM PM
XXX Offset (+HH:MM or Z) +09:00
VV Timezone ID Asia/Tokyo

Usage

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

LocalDateTime dt = LocalDateTime.of(2026, 2, 28, 14, 30, 0);
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String result = dt.format(fmt); // "2026-02-28 14:30:00"

// Parsing
LocalDateTime parsed = LocalDateTime.parse("2026-02-28 14:30:00", fmt);

Predefined Formatters

Java provides built-in formatters for common standards:

DateTimeFormatter.ISO_LOCAL_DATE       // 2026-02-28
DateTimeFormatter.ISO_LOCAL_DATE_TIME  // 2026-02-28T14:30:00
DateTimeFormatter.ISO_OFFSET_DATE_TIME // 2026-02-28T14:30:00+09:00
DateTimeFormatter.RFC_1123_DATE_TIME   // Sat, 28 Feb 2026 14:30:00 +0900

SimpleDateFormat Migration

Legacy code using SimpleDateFormat uses the same pattern letters but is not thread-safe. Migrate by replacing:

// Before (legacy, not thread-safe)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

// After (modern, thread-safe)
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

Use Case

Java DateTimeFormatter is used in Spring Boot REST APIs for date serialization, JPA entity timestamp formatting, log4j/logback date patterns, JDBC date handling, Apache Spark date parsing, and Android app date display. Its thread-safety makes it suitable for concurrent server applications.

Try It — Date Format Reference & Tester

Open full tool