In Java, users can input dates into an application in several formats, such as “YYYY-MM-DD”, “dd/MM/yyyy”, etc. Converting the input dates into a single standardized format like “dd/MM/yyyy”, simplifies the date manipulation operations. For this purpose, built-in classes like SimpleDateFormat and DateTimeFormatter are used in Java. This post will discuss different approaches for converting a date into a “dd/MM/yyyy” format.
How to Convert a Date to dd/MM/yyyy Format in Java Using SimpleDateFormat Class
Java offers a concrete class named “SimpleDateFormat” that helps us parse and format a date in a locale-sensitive manner. You can format a DateTime value into any specific(valid) pattern using this class. To convert a date into “dd/MM/yyyy” format in Java, you can use the constructor of the “SimpleDateFormat” class as follows:
SimpleDateFormat("dd/MM/yyyy");
This will convert the given date into “dd/MM/yyyy” format. Where “dd” and “MM” represent the day and month in 2 digits, while “yyyy” illustrates the year in 4 digits. It is important to note that, the date format must be exactly the same as we specify in the above signature. Otherwise, you may encounter faulty and unexpected results. To learn more about DateTime patterns, read the attached official documentation.
Example 1: Using SimpleDateFormat With Date Class
In the below code, first, we import the “SimpleDateFormat” and “Date” classes from the “java.text” and “java.util” packages respectively:
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatting {
public static void main(String[] args) {
Date currDate = new Date();
System.out.println("The Current Date is --> " + currDate);
SimpleDateFormat formatPattern = new SimpleDateFormat("dd/MM/yyyy");
String formatDate = formatPattern.format(currDate);
System.out.println("The Formatted Date is --> " + formatDate);
}
}
In the above-stated code,
- We get the current date and time using the Date() constructor and print it on the console.
- Next, we create an instance of the SimpleDateFormat class using its constructor and specify the “dd/MM/yyyy” format as a parameter.
- After this, we invoke the format() method on the current date to format it according to a valid date pattern, i.e., “dd/MM/yyyy”. This method will retrieve the string representation of the current DateTime in “dd/MM/yy” format.
- Finally, we print the formatted date on the console:
Example 2: Using the SimpleDateFormat with Calendar Class
Calendar is a built-in abstract class in Java that provides different Date and Time methods. One such method is getInstance() which retrieves an instance based on the current time zone of the Java runtime environment. You can use this method with the SimpleDateFormat() constructor to get the current date and convert it into “dd/MM/yyyy” format. Here’s the demonstration:
import java.text.SimpleDateFormat;
import java.util.*;
public class DateFormatting {
public static void main(String[] args) {
Calendar calObj = Calendar.getInstance();
SimpleDateFormat formatPattern = new SimpleDateFormat("dd/MM/yyyy");
String formatDate = formatPattern.format(calObj.getTime());
System.out.println("The Formatted Date is --> " + formatDate);
}
}
In the above code,
- First, we import the essential classes from their respective packages.
- In the main() method, we use the getInstance() method to initialize the calendar object “calObj” with the current DateTime.
- After this, we use the SimpleDateFormat() to specify the desired date pattern (i.e., dd/MM/yyyy).
- Finally, we use the format() method to format the given date object according to the specified pattern:
The output shows the formatted Date on the console:
How to Convert a Date to dd/MM/yyyy Format in Java Using the LocalDate and DateTimeFormatter Class
Alternatively, you can use the LocalDate class to get the current Date and the DateTimeFormatter class to convert the date into the “dd/MM/yyyy” format. Both these classes belong to the “java.time” package and must be imported before use:
import java.time.*;
import java.time.format.*;
public class DateFormatting {
public static void main(String[] args) {
LocalDate currDate = LocalDate.now();
System.out.println("The Current Date is --> " + currDate);
DateTimeFormatter formatPattern = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatDate = currDate.format(formatPattern);
System.out.println("The Formatted Date is --> " + formatDate);
}
}
In the main() method,
- First, we use the now() method to initialize the LocalDate object with the current date and print it on the console.
- After this, we use the ofPattern() method to specify a pattern to format the date.
- Finally, we use the format() method to format the currDate to the “dd/MM/yyyy” format and print the formatted date on the console:
That’s all about converting a date into “dd/MM/yyyy” in Java.
Final Thoughts
To convert a date to a “dd/MM/yyyy” format in Java, use the “SimpleDateFormat()” constructor or the “ofPattern()” method of the “DateTimeFormatter” class. Both the SimpleDateFormat() constructor and the “ofPattern()” method accept a String pattern “dd/MM/yyyy” as a parameter. After this, the String format() method is used/invoked on the given date to get a formatted date string in the “dd/MM/yyyy” format. This post has explained different approaches to converting a date into “dd/MM/yyyy” format.