DateTimeFormatter.ofPattern() / date.format()
A class that converts date-time values to formatted strings using a specified pattern, and parses strings back into date-time objects (Java 8+). DateTimeFormatter is used in combination with LocalDate, LocalDateTime, and similar classes.
Syntax
// Create a formatter with a format pattern.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
// Convert a date-time object to a string.
String str = dateTimeObject.format(formatter);
// Parse a string into a LocalDate.
LocalDate date = LocalDate.parse(string, formatter);
// Parse a string into a LocalDateTime.
LocalDateTime dateTime = LocalDateTime.parse(string, formatter);
Method and Pattern List
| Method / Pattern | Description |
|---|---|
| DateTimeFormatter.ofPattern(String) | Creates a formatter using the specified pattern string. |
| format(DateTimeFormatter) | Converts a date-time object to a string using the specified formatter. |
| LocalDate.parse(String, formatter) | Parses a string into a LocalDate using the specified formatter. |
| yyyy | Year (4 digits). Example: 2025 |
| MM | Month (2 digits, zero-padded). Example: 04 |
| dd | Day (2 digits, zero-padded). Example: 01 |
| HH | Hour (24-hour clock, 2 digits, zero-padded). Example: 14 |
| mm | Minute (2 digits, zero-padded). Example: 30 |
| ss | Second (2 digits, zero-padded). Example: 00 |
Sample Code
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// Format a LocalDate in a custom pattern.
LocalDate date = LocalDate.of(2025, 4, 1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String formatted = date.format(formatter);
System.out.println(formatted); // Prints "2025年04月01日"
// Format a LocalDateTime as a date-time string.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter dtFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
System.out.println(now.format(dtFormatter)); // e.g., "2025/04/01 14:30:00"
// Parse a string into a LocalDate.
String dateStr = "2025-12-25";
LocalDate christmas = LocalDate.parse(dateStr); // ISO format: no formatter needed.
System.out.println(christmas.getMonthValue()); // Prints "12"
// Parse with a custom pattern.
String customStr = "01/04/2025";
DateTimeFormatter customFmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate customDate = LocalDate.parse(customStr, customFmt);
System.out.println(customDate.getYear()); // Prints "2025"
// Use a predefined formatter.
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE)); // Prints "2025-04-01"
Notes
Pattern letters are case-sensitive. In particular, the month symbol MM outputs a number, MMM outputs an abbreviated name (e.g., Jan), and MMMM outputs the full name (e.g., January). An incorrect pattern string causes a DateTimeParseException to be thrown, so make sure the pattern matches the format of the input string exactly.
For ISO 8601 strings (yyyy-MM-dd), you can call LocalDate.parse(string) without providing a formatter.
For retrieving and creating dates, see LocalDate.now() / of() / getYear().
If you find any errors or copyright issues, please contact us.