← Back to Week 3 Hub

Date Format Patterns

A quick-reference for DateTimeFormatter pattern letters. Bookmark this page — you'll come back.

Case matters. MM is month; mm is minute. HH is 24-hour; hh is 12-hour. Mixing them up is the #1 date bug.

Pattern letters

LetterMeaningExamples
yYeary = 2026, yy = 26, yyyy = 2026
MMonthM = 4, MM = 04, MMM = Apr, MMMM = April
dDay of monthd = 9, dd = 09
DDay of yearD = 105 (day 105 of the year)
EDay of weekE = Wed, EEEE = Wednesday
HHour (0-23)H = 7, HH = 07
hHour (1-12)h = 3 (for 3 PM), hh = 03
mMinutem = 5, mm = 05
sSeconds = 9, ss = 09
SFraction of a secondSSS = 123 (milliseconds)
aAM / PM markera = AM or PM
zTime zonez = EST, zzzz = Eastern Standard Time
'Literal text (escape)"h 'o''clock'" → 3 o'clock

Ready-to-use patterns

MM/dd/yyyy
04/15/2026
yyyy-MM-dd
2026-04-15
dd-MMM-yyyy
15-Apr-2026
MMMM d, yyyy
April 15, 2026
EEEE, MMM dd, yyyy
Wednesday, Apr 15, 2026
HH:mm:ss
15:07:16
h:mm a
3:07 PM
yyyy-MM-dd HH:mm:ss
2026-04-15 15:07:16
yyyyMMdd
20260415 (good for filenames)
yyyyMMddHHmmss
20260415150716 (timestamp for filenames)
"h:mm 'on' dd-MMM-yyyy"
3:07 on 15-Apr-2026

Formatting a date (date → String)

import java.time.LocalDate; import java.time.format.DateTimeFormatter; LocalDate d = LocalDate.now(); DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy"); String s = d.format(fmt); // "04/15/2026"

Parsing a date (String → date)

String input = "04/15/2026"; DateTimeFormatter fmt = DateTimeFormatter.ofPattern("MM/dd/yyyy"); LocalDate d = LocalDate.parse(input, fmt);
Throws DateTimeParseException if the input doesn't match the pattern exactly. Wrap in try/catch if the input comes from a user.

Parsing in ISO format (no formatter needed)

// ISO 8601 format: YYYY-MM-DD LocalDate d = LocalDate.parse("2026-04-15"); // LocalDateTime ISO: YYYY-MM-DDTHH:mm:ss LocalDateTime dt = LocalDateTime.parse("2026-04-15T15:07:16");
← Common Errors Guide