Java Date Formatting Guide: Using SimpleDateFormat and DateFormat
Date and time operations in Java are critical to presenting data to the user in an appropriate format.
In this guide, you will learn the DateFormat and SimpleDateFormat classes in detail and see date/time formatting in different local settings (Locale) step by step.
💡 What You Will Learn in This Guide
- Difference between DateFormat and SimpleDateFormat
- Creating custom date/time patterns
- Locale formatting examples
- Use of
format()andparse()methods - Thread-safety warnings and modern
java.timealternative
🧠 Stage 1 – Basic of the Topic
Main technical topic: Date/time formatting and parsing in Java.
Problem it solves: Converts Date objects to the desired format or converts dates in text format to Date object.
Technical summary: This API handles Locale differences, displaying the date in a user-friendly format. However, it is not thread-safe.
📅 1. Regional Formatting with DateFormat
DateFormat formats dates according to the specified Locale value.
For example, while the date is 12/31/2025 in the USA, it appears as 31.12.2025 in Turkey.
🔧 Example: Date Formatting
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
Locale yerelAyar = new Locale.Builder().setLanguage("tr").setRegion("TR").build();
DateFormat tarihFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, yerelAyar);
String tarih = tarihFormat.format(new Date());
System.out.println(tarih);
💬 This code formats the date according to Türkiye locale.
Released: Oct 26, 2025
⏰ Example: Time Formatting Only
DateFormat zamanFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, yerelAyar);
String zaman = zamanFormat.format(new Date());
System.out.println(zaman);
💬 Displays time according to regional format.
Output: 22:53:01
📝 2. Custom Patterns with SimpleDateFormat
Unlike DateFormat, SimpleDateFormat allows you to define customized format patterns.
import java.text.SimpleDateFormat;
import java.util.Date;
String kalip = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(kalip);
String formatlanmisTarih = sdf.format(new Date());
System.out.println(formatlanmisTarih);
💬 Writes the date in yyyy-MM-dd HH:mm:ss format.
Released: 2025-10-26 22:53:01
📘 Pattern Syntax
The table below summarizes the most common letters used in date/time formatting 👇
| Pattern Letter | Description | Sample Output |
|---|---|---|
| y | Year (4 digits: yyyy) | 2025 |
| M | Month (Number: MM, Abbreviation: MMM, Full: MMMM) | 10 / Oct / October |
| d | Day of the month | 26 |
| E | Day of the week | Sun |
| H | Time of day (0-23) | 22 |
| m | Minute | 53 |
| s | Second | 01 |
| z | Time zone | GMT+03:00 |
| a | AM/PM indicator | PM |
| Z | RFC time difference | +0300 |
| X | ISO time difference | +03:00 |
🧩 3. Converting Date to Text (format)
The format() method returns the Date object as a String according to the defined pattern.
String kalip = "dd MMMM yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(kalip);
String tarih = sdf.format(new Date());
System.out.println(tarih);
💬 Returns the date in “day month year” format.
Released: October 26, 2025
🔄 4. Converting Text to Date (parse)
The parse() method converts the date in text (String) form into a Date object. The pattern must match the text exactly.
String kalip = "MM-dd-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(kalip);
String tarihMetni = "12-01-2025";
try {
Date tarihNesnesi = sdf.parse(tarihMetni);
System.out.println(tarihNesnesi);
} catch (Exception e) {
e.printStackTrace();
}
💬 Parses the date string as “12-01-2025”.
Released: Mon Dec 01 00:00:00 TRT 2025
🌍 5. Formatting by Locale
String kalip = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat sdf = new SimpleDateFormat(kalip, new Locale("fr", "FR"));
String tarih = sdf.format(new Date());
System.out.println(tarih);
💬 French date formatting example.
Released: mardi janvier 2025 14:51:02.354+0300
⚠️ Thread-Safety Warning
Both DateFormat and SimpleDateFormat classes are not thread-safe. In multi-threaded applications, you need to create a separate instance for each thread.
🧭 6. Modern Alternative: java.time API
For Java 8 and later, the java.time package (LocalDate, LocalDateTime, DateTimeFormatter) is recommended. This structure is thread-safe, modern and high-performance.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
LocalDateTime simdi = LocalDateTime.now();
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
System.out.println(simdi.format(format));
💬 Secure date formatting with new API.
Released: 26-10-2025 22:53:01
❓ Frequently Asked Questions (FAQ)
- Why is SimpleDateFormat not thread-safe?
There is a shared Calendar object in it. Data conflict occurs in multiple access.
- How to get date in yyyy-MM-dd format?
new SimpleDateFormat("yyyy-MM-dd").format(new Date());
- What is the difference between MM and mm?
MM → Month, mm → Minute. The case difference is important.
- What should I do to convert the date from DD MM YYYY to MM DD YYYY?
First read the existing text with parse(), then convert it to the new pattern with format().
- Which class is recommended in modern Java?
java.time.format.DateTimeFormatter → thread-safe, modern and easy to read.
🧾 Result
SimpleDateFormat and DateFormat are Java's classic date formatting tools. However, in new projects, java.time API should be preferred. In this way, results are obtained that are both error-free and safe in multi-threaded environments.
💡 You can instantly test these examples by running your Java applications on GenixNode. Try modern date operations on GenixNode infrastructure, feel the difference 🚀

