Java – Get Current Date in MM/DD/YY Format
To get current date in MM/DD/YY format in Java, where MM/DD/YY is Month/Day/Year
- Import LocalDate class from java.time package.
- Create LocalDate object by calling static method now() of LocalDate class. LocalDate.now() method returns the current date from the system clock in the default time zone.
- Create DateTimeFormatter from DateTimeFormatter.ofPattern() method and pass the pattern
"MM/dd/yy"
as argument to ofPattern() method. - Call format() method on this LocalDate object with DateTimeFormatter object passed as argument. The format() method returns a string with the date formatted in the given pattern
"MM/dd/yy"
.
Example
In the following program, we shall use LocalDate and DateTime classes to format date in the pattern "MM/dd/yy"
.
Java Program
</>
Copy
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Example {
public static void main(String[] args) {
LocalDate dateObj = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yy");
String date = dateObj.format(formatter);
System.out.println(date);
}
}
Output
02/18/21
format() method returned the date in the specified pattern.
Conclusion
In this Java Tutorial, we learned how to get current date in MM/DD/YY format using LocalDate and DateTimeFormatter classes of java.time package, with example program.