Home Java LocalDateTime Methods format() 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().
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" Call What 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.time.LocalDateTime
public String format(DateTimeFormatter formatter) Parameters Parameter Description formatter (DateTimeFormatter)The formatter that decides how the text looks. It must not be null.
Return Value Return Value Description StringThe formatted date-time text.
Important
The case of the pattern letters matters: MM is the month, but mm is the minute. HH is the hour from 0 to 23, but hh is from 1 to 12 and needs a for AM or PM. A pattern that needs a zone, such as z, throws an UnsupportedTemporalTypeException, because a LocalDateTime has no zone. A null formatter throws a NullPointerException.
Compare with Similar Methods Other ways to get text from a date-time:
Java Examples 1. Common patterns 2. Words and 12-hour 3. Traps and round trip 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.
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));
}
} 15/03/2026
2026-03-15 14:30:45
15 Mar 2026 How It Works One date-time. It is 15 March 2026 at 14:30:45.Day, month, year. dd/MM/yyyy uses two digits for the day and the month, and four for the year.Date and 24-hour time. HH:mm:ss is hours from 00 to 23, minutes, and seconds.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.
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));
}
} Sunday, March 15, 2026
2:30 PM
02:30 PM How It Works Full names. EEEE is the full day name and MMMM is the full month name. A single d has no leading zero.12-hour clock. h is the hour from 1 to 12, and a adds AM or PM. 14:30 becomes 2:30 PM.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.
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));
}
} wrong (mm): 2026-30-15
right (MM): 2026-03-15
15/03/2026 14:30 parsed back equals original: true How It Works The wrong pattern. Lowercase mm means minutes, so the month position shows 30, the minutes of 14:30.The right pattern. Uppercase MM shows the month, 03.Format and parse with one formatter. The same formatter turns the date-time into text and back.A perfect round trip. The pattern keeps all the parts of this date-time, so the parsed value equals the original.Frequently Asked Questions How do I format a LocalDateTime in Java? Create a formatter with DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm") and call dt.format(formatter).
What is the difference between MM and mm in a pattern? MM is the month, and mm is the minute. Mixing them up is the most common mistake.
How do I show AM and PM? Use hh or h for the hour, and add a for the AM or PM marker, for example "hh:mm a".
Why does format() throw UnsupportedTemporalTypeException? 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.
Is DateTimeFormatter thread-safe? Yes. Unlike the old SimpleDateFormat, one formatter can be shared by many threads.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion