Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. DateTimeFormatter.ofPattern() / date.format()

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 / PatternDescription
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.
yyyyYear (4 digits). Example: 2025
MMMonth (2 digits, zero-padded). Example: 04
ddDay (2 digits, zero-padded). Example: 01
HHHour (24-hour clock, 2 digits, zero-padded). Example: 14
mmMinute (2 digits, zero-padded). Example: 30
ssSecond (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 .