Java – Get Current Date in MM/DD/YYYY Format

To get current date in MM/DD/YYYY format in Java, where MM/DD/YYYY is Month/Day/Year

  1. Import LocalDate class from java.time package.
  2. 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.
  3. Create DateTimeFormatter from DateTimeFormatter.ofPattern() method and pass the pattern "MM/dd/yyyy" as argument to ofPattern() method.
  4. 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/yyyy".

Example

In the following program, we shall use LocalDate and DateTime classes to format date in the pattern "MM/dd/yyyy".

Java Program

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/yyyy");
		String date = dateObj.format(formatter);
		System.out.println(date);
	}

}

Output

02/18/2021

format() method returned the date in the specified pattern.

ADVERTISEMENT

Conclusion

In this Java Tutorial, we learned how to get current date in MM/DD/YYYY format using LocalDate and DateTimeFormatter classes of java.time package, with example program.