← Back to Week 3 Hub

DateTimeFormatter Playground

Build a pattern, see the output. Or take a formatted string and parse it back into a LocalDate. Two directions, one tool.

DateTimeFormatter lives in java.time.format. You build one with DateTimeFormatter.ofPattern("...") using letter codes below, then pass it to format() to output a string — or to parse() to turn a string into a date.
Playground
Try a preset:
Output string
LocalDateTime dt = LocalDateTime.parse(""); DateTimeFormatter fmt = DateTimeFormatter.ofPattern(""); String out = dt.format(fmt); // → ""
Try a preset (input & pattern together):
Parsed LocalDate
String input = ""; DateTimeFormatter fmt = DateTimeFormatter.ofPattern(""); LocalDate d = LocalDate.parse(input, fmt); // →
Heads-up: if the input doesn't match the pattern, Java throws DateTimeParseException. Bad month, wrong separator, extra text — all throw.

Pattern letters — reference

Tip: the letters are case-sensitive. MM means month, mm means minute — don't mix them up (this is the #1 date format bug).

← LocalDate / LocalTime / LocalDateTime Array vs ArrayList →