In Java (starting from Java 8), you can format dates using the DateTimeFormatter class, which provides an easier and more modern approach to date and time formatting. This class is part of the java.time.format package and works seamlessly with the java.time API (e.g., LocalDate, LocalDateTime, ZonedDateTime).
Here’s how you can format dates with DateTimeFormatter:
Example of Formatting Dates with DateTimeFormatter
package org.kodejava.datetime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTimeFormatterExample {
public static void main(String[] args) {
// Get the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();
// Define a formatter with a custom pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
// Format the date and time
String formattedDateTime = currentDateTime.format(formatter);
// Print the result
System.out.println("Formatted Date and Time: " + formattedDateTime);
}
}
Example Output:
Formatted Date and Time: 02/08/2025 15:52:30
Common Patterns for Date and Time
Here are the most commonly used symbols for formatting patterns with DateTimeFormatter:
| Symbol | Meaning | Example |
|---|---|---|
y |
Year | 2025 |
M |
Month | 08 or August |
d |
Day of the month | 02 |
E |
Day name in a week | Tue |
H |
Hour (0-23) | 15 |
h |
Hour (1-12, AM/PM) | 3 |
m |
Minute in hour | 45 |
s |
Second in minute | 30 |
a |
AM/PM | PM |
z |
Time zone name | PDT |
'text' |
Literal text | ‘at’ |
Example with Fully Custom Pattern
package org.kodejava.datetime;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class CustomDateFormatting {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
// Custom date format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMMM dd yyyy");
String formattedDate = currentDate.format(formatter);
// Print the formatted date
System.out.println("Custom Formatted Date: " + formattedDate);
}
}
Output:
Custom Formatted Date: Saturday, August 02 2025
Predefined Formatters in DateTimeFormatter
DateTimeFormatter also provides several predefined, common formatters:
| Formatter | Pattern | Example |
|---|---|---|
DateTimeFormatter.ISO_DATE |
yyyy-MM-dd |
2025-08-02 |
DateTimeFormatter.ISO_TIME |
HH:mm:ss |
15:45:30 |
DateTimeFormatter.ISO_DATE_TIME |
yyyy-MM-dd'T'HH:mm:ss |
2025-08-02T15:45:30 |
DateTimeFormatter.BASIC_ISO_DATE |
yyyyMMdd |
20250802 |
DateTimeFormatter.RFC_1123_DATE_TIME |
RFC 1123 format | Sat, 02 Aug 2025 15:45:30 |
Example: Formatting Dates with Time Zones
package org.kodejava.datetime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class ZonedDateTimeFormatterExample {
public static void main(String[] args) {
// Get the current date and time with time zone
ZonedDateTime zonedDateTime = ZonedDateTime.now();
// Define a formatter with a custom pattern that includes the time zone
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss z Z");
// Format the ZonedDateTime
String formattedDateTime = zonedDateTime.format(formatter);
// Print the result
System.out.println("Formatted Date and Time with Time Zone: " + formattedDateTime);
}
}
Output:
Formatted Date and Time with Time Zone: 02/08/2025 15:50:30 PDT -0700
Explanation of Pattern:
z: Displays the short name of the time zone (e.g.,PDT,GMT).Z: Displays the time zone offset (e.g.,-0700).
Parsing and Formatting Specific Time Zones
You can work with specific time zones using the ZoneId class:
package org.kodejava.datetime;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class SpecificTimeZoneExample {
public static void main(String[] args) {
// Get the current date and time in a specific time zone
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/London"));
// Define a formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, MMM dd yyyy HH:mm:ss z");
// Format the ZonedDateTime
String formattedDateTime = zonedDateTime.format(formatter);
// Print the result
System.out.println("London Time: " + formattedDateTime);
}
}
Output:
London Time: Saturday, Aug 02 2025 23:50:30 BST
Predefined Formatter for Time Zones
If you’d like to use the predefined formatters to format dates with time zones, you can try:
package org.kodejava.datetime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class PredefinedTimeZoneFormatter {
public static void main(String[] args) {
// Get the current ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.now();
// Use a predefined formatter
String formattedDateTime = zonedDateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME);
// Print the result
System.out.println("Formatted with Predefined Formatter: " + formattedDateTime);
}
}
Output:
Formatted with Predefined Formatter: Sat, 02 Aug 2025 15:50:30 -0700
Notes:
-
Thread Safety:
UnlikeSimpleDateFormat,DateTimeFormatteris thread-safe and can be safely used in concurrent environments. -
Extensible Patterns:
You can use literal text in the patterns by enclosing it in single quotes ('text'). -
Working with Time Zones:
If you are working with time zones, you can useZonedDateTimeorOffsetDateTimealong with aDateTimeFormatter. -
ZoneIdUse:
You can specify almost any valid time zone usingZoneId.of("Zone_Name"). Example:"America/New_York","Asia/Tokyo","Australia/Sydney". - Daylight Saving Time:
Time zones take daylight saving time into account automatically if applicable. - Predefined Formatters with Zones:
Predefined formatters likeDateTimeFormatter.ISO_ZONED_DATE_TIMEandDateTimeFormatter.RFC_1123_DATE_TIMEare handy for common time zone formats.
