Java LocalDateTime format() Method
Example
Show a date and time without the T:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.of(2026, 1, 15, 14, 30);
System.out.println("Before formatting: " + myDateObj);
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = myDateObj.format(myFormatObj);
System.out.println("After formatting: " + formattedDate);
}
}
Definition and Usage
The format() method returns the date and time as a String, in the format that a DateTimeFormatter describes.
Create the formatter with DateTimeFormatter.ofPattern(), for example with the pattern "dd-MM-yyyy HH:mm:ss".
Tip: See the ofPattern() method for the letters you can use in a pattern.
Syntax
public String format(DateTimeFormatter formatter)
Parameter Values
| Parameter | Description |
|---|---|
| formatter | Required. The DateTimeFormatter that describes the format. |
Technical Details
| Returns: | The formatted date and time, as a String. |
|---|
Related Pages
❮ Date and Time Methods