Java Programs›Date & Time›Format a Date

Format a Date in Java

beginner·  Date & Time  ·  Date & Time

Problem

Formatting turns a date object into text; a pattern string such as dd/MM/yyyy says which fields appear, in what order and in what style.

Display the date-time 9 March 2026, 14:05 in several common formats.

Input
2026-03-09T14:05
Output
09/03/2026, March 9, 2026, 02:05 PM, ...

Java Program

Java
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; public class FormatDate { public static void main(String[] args) { LocalDateTime meeting = LocalDateTime.of(2026, 3, 9, 14, 5); // 9 March 2026, 14:05 String[] patterns = {"dd/MM/yyyy", "MMMM d, yyyy", "EEE, d MMM yyyy", "hh:mm a", "yyyy-MM-dd HH:mm"}; for (String pattern : patterns) { DateTimeFormatter f = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH); // fixed language for names System.out.println(pattern + " -> " + meeting.format(f)); } } }

Output

dd/MM/yyyy -> 09/03/2026 MMMM d, yyyy -> March 9, 2026 EEE, d MMM yyyy -> Mon, 9 Mar 2026 hh:mm a -> 02:05 PM yyyy-MM-dd HH:mm -> 2026-03-09 14:05

Core Logic

One LocalDateTime value can be rendered any number of ways — each DateTimeFormatter pattern is just a different view of the same moment.

How It Works
  1. 1LocalDateTime.of(2026, 3, 9, 14, 5) creates a fixed date-time.
  2. 2DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH) builds a formatter; the locale fixes the language of month and day names.
  3. 3meeting.format(f) produces the text for each pattern.
  4. 4Pattern letters: dd day, MM month number, MMMM month name, EEE short weekday, hh/HH 12-/24-hour clock, a AM/PM.
hh:mm a prints 02:05 PM, while HH:mm prints 14:05 for the same time.
💡

Key Point: Case matters: MM is month but mm is minutes, and YYYY is the week-based year, which differs from yyyy around New Year.

Key Concepts

LocalDateTimeDateTimeFormatter.ofPattern()Localepattern letters

Approach 2: Built-in ISO Formatters

Java
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class FormatDateIso { public static void main(String[] args) { LocalDateTime meeting = LocalDateTime.of(2026, 3, 9, 14, 5); System.out.println("toString(): " + meeting); System.out.println("ISO_LOCAL_DATE: " + meeting.format(DateTimeFormatter.ISO_LOCAL_DATE)); System.out.println("ISO_LOCAL_TIME: " + meeting.format(DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println("BASIC_ISO_DATE: " + meeting.format(DateTimeFormatter.BASIC_ISO_DATE)); } }

Output

toString(): 2026-03-09T14:05 ISO_LOCAL_DATE: 2026-03-09 ISO_LOCAL_TIME: 14:05:00 BASIC_ISO_DATE: 20260309

Core Logic

For machine-readable output, the predefined ISO formatters give standard text with no pattern to get wrong.

How It Works
  1. 1toString() on a LocalDateTime already returns ISO-8601 text.
  2. 2ISO_LOCAL_DATE and ISO_LOCAL_TIME print just the date or just the time.
  3. 3BASIC_ISO_DATE produces the compact yyyyMMdd form used in file names.
ISO_LOCAL_TIME includes seconds, printing 14:05:00.
💡

Key Point: Store and exchange dates in ISO format, and apply locale-specific patterns only when showing them to people.

Key Concepts

ISO_LOCAL_DATEISO_LOCAL_TIMEBASIC_ISO_DATEtoString()

Interview Questions

Is DateTimeFormatter thread-safe?

Yes. Unlike the old SimpleDateFormat, a DateTimeFormatter is immutable and can be shared between threads as a constant.

How do you include the time zone in formatted output?

Format a ZonedDateTime and use pattern letters such as z (zone name) or XXX (offset).

Learn the Concept

Related Programs