Java LocalDateTime format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

In Java, dt.format(formatter) converts a LocalDateTime into a String. The pattern, such as "dd/MM/yyyy HH:mm", tells Java how the text should look.

You build the formatter once with DateTimeFormatter.ofPattern(), and then you can use it again and again. The formatter is thread-safe, so it can be shared.

An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file.

Syntax

Create a DateTimeFormatter from a pattern, and pass it to format().

Java
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");

String text = dt.format(formatter);   // "15/03/2026 14:30"
CallWhat it does
dt.format(formatter)Returns the text for the date-time.
DateTimeFormatter.ofPattern(p)Creates a formatter from a pattern such as "yyyy-MM-dd".
formatter.format(dt)The same result, written the other way around.
LocalDateTime.parse(text, formatter)Turns the text back into a date-time.

Add Locale.ENGLISH to ofPattern() when the text has month or day names, so that the output is the same on every computer.

Library declarations
Java
// java.time.LocalDateTime
public String format(DateTimeFormatter formatter)

Parameters

ParameterDescription
formatter (DateTimeFormatter)The formatter that decides how the text looks. It must not be null.

Return Value

Return ValueDescription
StringThe formatted date-time text.

Compare with Similar Methods

Other ways to get text from a date-time:

MethodHow it differs
toString()Gives the fixed ISO-8601 text, with no choice of pattern.
LocalDateTime.parse(text, formatter)The opposite direction: text to date-time.
toLocalDate()Gets only the date part, which you can format on its own.

Java Examples

Example 1 — Format a Date-Time with Common Patterns

Print the same date-time as a European date, as a database-style value, and with a short month name.

Java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class LdtFormatCommon {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30, 45);

        DateTimeFormatter eu = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        DateTimeFormatter db = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter shortMonth = DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ENGLISH);

        System.out.println(dt.format(eu));
        System.out.println(dt.format(db));
        System.out.println(dt.format(shortMonth));
    }
}

How It Works

  1. One date-time. It is 15 March 2026 at 14:30:45.
  2. Day, month, year. dd/MM/yyyy uses two digits for the day and the month, and four for the year.
  3. Date and 24-hour time. HH:mm:ss is hours from 00 to 23, minutes, and seconds.
  4. A month name. MMM gives the short name, Mar. The locale makes sure it is in English.

Example 2 — Format with Day Names and a 12-Hour Clock

Show the long date with the day name, and the time on a 12-hour clock with AM or PM.

Java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class LdtFormatWords {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);

        DateTimeFormatter longDate = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy", Locale.ENGLISH);
        DateTimeFormatter clock = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
        DateTimeFormatter padded = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);

        System.out.println(dt.format(longDate));
        System.out.println(dt.format(clock));
        System.out.println(dt.format(padded));
    }
}

How It Works

  1. Full names. EEEE is the full day name and MMMM is the full month name. A single d has no leading zero.
  2. 12-hour clock. h is the hour from 1 to 12, and a adds AM or PM. 14:30 becomes 2:30 PM.
  3. Padding. hh always uses two digits, so the hour becomes 02.

Example 3 — Avoid the mm Trap and Parse the Text Back

Use a wrong pattern on purpose, then format and parse back with the correct one.

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

public class LdtFormatTraps {
    public static void main(String[] args) {
        LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);

        System.out.println("wrong (mm): " + dt.format(DateTimeFormatter.ofPattern("yyyy-mm-dd")));
        System.out.println("right (MM): " + dt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
        String text = dt.format(formatter);
        LocalDateTime back = LocalDateTime.parse(text, formatter);
        System.out.println(text + " parsed back equals original: " + back.equals(dt));
    }
}

How It Works

  1. The wrong pattern. Lowercase mm means minutes, so the month position shows 30, the minutes of 14:30.
  2. The right pattern. Uppercase MM shows the month, 03.
  3. Format and parse with one formatter. The same formatter turns the date-time into text and back.
  4. A perfect round trip. The pattern keeps all the parts of this date-time, so the parsed value equals the original.

Frequently Asked Questions

Create a formatter with DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm") and call dt.format(formatter).
MM is the month, and mm is the minute. Mixing them up is the most common mistake.
Use hh or h for the hour, and add a for the AM or PM marker, for example "hh:mm a".
The pattern asks for something that a LocalDateTime does not have, such as a time zone (z, Z, or VV). Use a ZonedDateTime for those.
Yes. Unlike the old SimpleDateFormat, one formatter can be shared by many threads.

Next: from()

Convert another date-time object into a LocalDateTime.

from() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful