0Pricing
Java Academy · Lesson

Formatting with DateTimeFormatter

Parse and format dates.

Formatting with DateTimeFormatter is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Turning Dates Into Text

The default toString of a date prints in ISO format. To control the appearance, or to parse non-ISO text, use DateTimeFormatter from the java.time.format package.

A Custom Pattern

Create a formatter with ofPattern. Letters stand for fields: yyyy for year, MM for month, dd for day.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        System.out.println(date.format(f));
    }
}

Formatting Time

Time patterns use HH for 24-hour hours, mm for minutes, and ss for seconds.

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(14, 5, 30);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm:ss");
        System.out.println(time.format(f));
    }
}

Combining Date and Time

One pattern can include both date and time fields for a LocalDateTime.

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

public class Main {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 5, 30, 9, 45);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        System.out.println(dt.format(f));
    }
}

Text Month Names

Repeating the month letter changes its form: MM is a number, MMM is a short name like May, and MMMM is the full name.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMMM yyyy", Locale.ENGLISH);
        System.out.println(date.format(f));
    }
}

Day of Week Names

Use EEEE for the full weekday name and EEE for the short form. Provide a Locale for predictable language.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("EEEE, dd MMM", Locale.ENGLISH);
        System.out.println(date.format(f));
    }
}

Twelve-Hour Clock

Use lowercase hh for 12-hour hours and a for the AM or PM marker.

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.of(15, 30);
        DateTimeFormatter f = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
        System.out.println(time.format(f));
    }
}

Parsing With a Pattern

The same formatter parses text back into a date. Pass it as the second argument to parse.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        LocalDate date = LocalDate.parse("30/05/2026", f);
        System.out.println(date.getMonth());
    }
}

Handling Bad Input

If the text does not match the pattern, parsing throws a DateTimeParseException. Wrap risky parsing in a try-catch.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter f = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        try {
            LocalDate.parse("not a date", f);
        } catch (DateTimeParseException e) {
            System.out.println("Invalid date format!");
        }
    }
}

Built-In Formatters

For common needs you can skip patterns and use predefined formatters like DateTimeFormatter.ISO_DATE.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 5, 30);
        System.out.println(date.format(DateTimeFormatter.ISO_DATE));
    }
}

Reuse Your Formatters

DateTimeFormatter is immutable and thread-safe, so create it once and reuse it. Always supply a Locale when names are involved to keep output consistent.

Quick Check

Test your formatting knowledge.

Recap

You learned date and time formatting:

  • DateTimeFormatter.ofPattern defines custom output
  • Letters map to fields: yyyy, MM, dd, HH, mm
  • More repeated letters give text forms like MMMM and EEEE
  • The same formatter parses text, throwing DateTimeParseException on bad input
  • Formatters are immutable and reusable; pass a Locale for names

Frequently asked questions

Is the “Formatting with DateTimeFormatter” lesson free?

Yes — the full text of “Formatting with DateTimeFormatter” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Formatting with DateTimeFormatter”?

Parse and format dates. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Formatting with DateTimeFormatter” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. LocalDate, LocalTime, LocalDateTime
  2. Instant and Duration
  3. Period and Date Arithmetic
  4. Formatting with DateTimeFormatter
← Back to Java Academy