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")wherepatternis your date format. - SimpleDateFormat: Create an instance with
new SimpleDateFormat("pattern")and callformat(date).
Common pattern symbols include:
yyyy- yearMM- month numberdd- day of monthHH- hour (24-hour)mm- minutesss- 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
SimpleDateFormatwithout considering thread safety (it is not thread-safe). - Confusing pattern letters, for example, using
mmfor months instead ofMM(wheremmmeans minutes). - Not using the modern
DateTimeFormatterwhich 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 Symbol | Meaning | Example |
|---|---|---|
| yyyy | Year (4 digits) | 2024 |
| MM | Month (2 digits) | 06 |
| dd | Day of month (2 digits) | 15 |
| HH | Hour in 24h format (2 digits) | 14 |
| mm | Minutes (2 digits) | 30 |
| ss | Seconds (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.