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
| Letter | Meaning | Examples |
| y | Year | y = 2026, yy = 26, yyyy = 2026 |
| M | Month | M = 4, MM = 04, MMM = Apr, MMMM = April |
| d | Day of month | d = 9, dd = 09 |
| D | Day of year | D = 105 (day 105 of the year) |
| E | Day of week | E = Wed, EEEE = Wednesday |
| H | Hour (0-23) | H = 7, HH = 07 |
| h | Hour (1-12) | h = 3 (for 3 PM), hh = 03 |
| m | Minute | m = 5, mm = 05 |
| s | Second | s = 9, ss = 09 |
| S | Fraction of a second | SSS = 123 (milliseconds) |
| a | AM / PM marker | a = AM or PM |
| z | Time zone | z = EST, zzzz = Eastern Standard Time |
| ' | Literal text (escape) | "h 'o''clock'" → 3 o'clock |
Ready-to-use patterns
MMMM d, yyyy
April 15, 2026
EEEE, MMM dd, yyyy
Wednesday, Apr 15, 2026
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");