Dates can be represented in different formats depending on the application, region, or user preference. Java provides the java.time package to work with dates and times and allows us to easily format a date using DateTimeFormatter.
- LocalDate is used when we only need the date without time or timezone information.
- DateTimeFormatter is used to format a date according to a specified pattern.
- The ofPattern() method allows us to create custom date formats.
Example: Program to formats the same LocalDate using two different patterns.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class GFG {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2026, 9, 4);
DateTimeFormatter format1 =
DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter format2 =
DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy");
System.out.println(date.format(format1));
System.out.println(date.format(format2));
}
}
Output
04/09/2026 Friday, 04 September 2026
Explanation: The program creates a LocalDate and uses DateTimeFormatter.ofPattern() to format it in two different date formats.
Ways to Format Date in Java
Java provides LocalDate and DateTimeFormatter to display dates in default and custom formats such as yyyy-MM-dd, dd/MM/yyyy, and formats containing day or month names.
1. Display Date in yyyy-MM-dd Format
The LocalDate.now() method returns the current date. By default, LocalDate displays the date in the yyyy-MM-dd format.
Approach
- Import the LocalDate class.
- Use LocalDate.now() to get the current date.
- Store the date in a LocalDate object.
- Print the date.
import java.time.LocalDate;
public class DateFormatExample {
public static void main(String[] args) {
// Get the current date
LocalDate date = LocalDate.now();
// Print the date
System.out.println("Current date: " + date);
}
}
Output
Current date: 2026-08-26
Explanation: LocalDate.now() returns the current date according to the system's default timezone.
2. Display Date in dd/MM/yyyy Format
We can use DateTimeFormatter when we want to display the date in a custom format.
Approach
- Create a LocalDate object using LocalDate.now().
- Create a DateTimeFormatter using ofPattern().
- Specify the required pattern as dd/MM/yyyy.
- Use the format() method to format the date.
- Print the formatted date.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatExample {
public static void main(String[] args)
{
// Get the current date
LocalDate date = LocalDate.now();
// Specify the required date format
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("dd/MM/yyyy");
// Format the date
String formattedDate = date.format(formatter);
// Print the formatted date
System.out.println("Formatted date: "
+ formattedDate);
}
}
Output
Formatted date: 26/08/2026
Explanation: The program uses LocalDate.now() to get the current date and DateTimeFormatter to define the required format as dd/MM/yyyy. The format() method converts the date into the specified format and displays it as a string.
3. Display Date with Day and Month Name
We can also include the name of the day and the short name of the month.
Approach
- Get the current date using LocalDate.now().
- Create a DateTimeFormatter.
- Use EEEE to display the full day name.
- Use dd to display the day of the month.
- Use MMM to display the short month name.
- Format and print the date.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatExample {
public static void main(String[] args) {
// Get the current date
LocalDate date = LocalDate.now();
// Specify the required date format
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEEE, dd MMM yyyy");
// Format the date
String formattedDate = date.format(formatter);
// Print the formatted date
System.out.println("Formatted date: " + formattedDate);
}
}
Output
Formatted date: Wednesday, 26 Aug 2026
Explanation: The program gets the current date using `LocalDate.now()` and uses `DateTimeFormatter` with the pattern **"EEEE, dd MMM yyyy"** to format it. The `format()` method converts the date into a readable form containing the **day name, date, and month name**, such as 'Wednesday, 26 Aug 2026'.
Date and Time API
Java provides the java.time package to work with dates and times. It includes classes for handling dates, times, time zones, and date-time operations.
| Class | Description |
|---|---|
LocalDate | Represents a date without time or timezone, such as 2026-08-26. |
LocalTime | Represents a time without a date or timezone. |
LocalDateTime | Represents both date and time without a timezone. |
DateTimeFormatter | Used to format and parse date-time values. |
ZonedDateTime | Represents date and time with a timezone. |
Instant | Represents a specific moment on the timeline. |
Period | Represents a date-based amount of time such as years, months, and days. |
Duration | Represents a time-based amount such as hours, minutes, and seconds. |
Date Format Symbols in Java
The DateTimeFormatter.ofPattern() method uses predefined symbols to specify how the date should be displayed. Some commonly used symbols are:
| Symbol | Meaning | Example |
|---|---|---|
yyyy | Four-digit year | 2026 |
yy | Two-digit year | 26 |
MM | Month as a number | 08 |
MMM | Short month name | Aug |
MMMM | Full month name | August |
dd | Day of the month | 26 |
EEE | Short day name | Wed |
EEEE | Full day name | Wednesday |
Note: Date-format symbols are case-sensitive. For example, MM represents the month, while mm represents minutes.Different Ways to Display Dates