Bird
Raised Fist0
JavaHow-ToBeginner · 3 min read

How to Format Date in Java: Simple Guide with Examples

In Java, you can format dates using DateTimeFormatter for modern code or SimpleDateFormat for legacy code. Use a pattern string like "yyyy-MM-dd" to specify the date format you want.
📐

Syntax

To format dates in Java, you use a formatter with a pattern string that defines how the date should look. The modern way is with DateTimeFormatter and the legacy way is with SimpleDateFormat.

  • DateTimeFormatter: Use DateTimeFormatter.ofPattern("pattern") where pattern is your date format.
  • SimpleDateFormat: Create an instance with new SimpleDateFormat("pattern") and call format(date).

Common pattern symbols include:

  • yyyy - year
  • MM - month number
  • dd - day of month
  • HH - hour (24-hour)
  • mm - minutes
  • ss - seconds
java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;

// Modern way
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// Legacy way
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
💻

Example

This example shows how to format the current date and time using both DateTimeFormatter and SimpleDateFormat. It prints the formatted date as a string.

java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatExample {
    public static void main(String[] args) {
        // Modern approach
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        System.out.println("Formatted date and time (modern): " + formattedDateTime);

        // Legacy approach
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        System.out.println("Formatted date and time (legacy): " + formattedDate);
    }
}
Output
Formatted date and time (modern): 2024-06-15 14:30:45 Formatted date and time (legacy): 2024-06-15 14:30:45
⚠️

Common Pitfalls

Common mistakes when formatting dates in Java include:

  • Using SimpleDateFormat without considering thread safety (it is not thread-safe).
  • Confusing pattern letters, for example, using mm for months instead of MM (where mm means minutes).
  • Not using the modern DateTimeFormatter which is thread-safe and recommended since Java 8.
java
import java.text.SimpleDateFormat;
import java.util.Date;

public class PitfallExample {
    public static void main(String[] args) {
        Date date = new Date();

        // Wrong pattern: 'mm' means minutes, not month
        SimpleDateFormat wrongFormat = new SimpleDateFormat("yyyy-mm-dd");
        System.out.println("Wrong format: " + wrongFormat.format(date));

        // Correct pattern: 'MM' means month
        SimpleDateFormat correctFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("Correct format: " + correctFormat.format(date));
    }
}
Output
Wrong format: 2024-30-15 Correct format: 2024-06-15
📊

Quick Reference

Pattern SymbolMeaningExample
yyyyYear (4 digits)2024
MMMonth (2 digits)06
ddDay of month (2 digits)15
HHHour in 24h format (2 digits)14
mmMinutes (2 digits)30
ssSeconds (2 digits)45
✅

Key Takeaways

Use DateTimeFormatter with a pattern string to format dates in modern Java.
SimpleDateFormat is legacy and not thread-safe; prefer DateTimeFormatter when possible.
Pattern letters are case-sensitive: 'MM' is month, 'mm' is minutes.
Always test your date format output to avoid confusion.
DateTimeFormatter is thread-safe and recommended since Java 8.