DateTimeFormatter

The DateTimeFormatter class is used to format and parse date-time objects in Java. It supports predefined standard formats, custom patterns, and locale-sensitive formatting, making it essential for applications involving date/time I/O.

Commonly Used Methods

Simple Program

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

public class FormatterExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        
        DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String formatted = now.format(customFormatter);

        System.out.println("Current Date-Time (formatted): " + formatted);
    }
}

LotusJavaPrince and Mahesh are working on a global banking portal that formats user transaction timestamps according to different countries’ locales and display styles. They want:

  • ISO and custom pattern formatting.
  • Locale-based formatting (e.g., French, US).
  • Parsing user-inputted date strings back to LocalDate.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class FormatterCaseStudy {
    public static void main(String[] args) {
        System.out.println("Date-Time Formatter Portal - by LotusJavaPrince & Mahesh");

        // 1. Custom Format
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter customFmt = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy HH:mm:ss");
        System.out.println("Custom Format: " + now.format(customFmt));

        // 2. ISO Format
        DateTimeFormatter isoFmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        System.out.println("ISO Format: " + now.format(isoFmt));

        // 3. Locale-based Format (US & France)
        DateTimeFormatter usFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.US);
        DateTimeFormatter frFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.FRANCE);

        LocalDate date = LocalDate.of(2025, 5, 22);
        System.out.println("US Locale: " + date.format(usFormatter));
        System.out.println("FR Locale: " + date.format(frFormatter));

        // 4. Parsing User Input
        String inputDate = "22-05-2025";
        DateTimeFormatter parseFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        LocalDate parsedDate = LocalDate.parse(inputDate, parseFormatter);
        System.out.println("Parsed Date: " + parsedDate);
    }
}
/*
Date-Time Formatter Portal - by LotusJavaPrince & Mahesh
Custom Format: Thursday, May 22 2025 14:50:05
ISO Format: 2025-05-22T14:50:05.123
US Locale: Thursday, May 22, 2025
FR Locale: jeudi 22 mai 2025
Parsed Date: 2025-05-22
*/

The DateTimeFormatter class is a core utility in the java.time API for:

  • Formatting LocalDate, LocalTime, and LocalDateTime objects.
  • Supporting standard and custom patterns.
  • Enabling locale-aware formatting.
  • Parsing strings into date-time objects, which is critical for user input validation.
Scroll to Top