Write a Java program to print LocalDate yyyy-MM-dd


This Java program retrieves the current system date and formats it as a string in the pattern "yyyy-MM-dd". The LocalDate class from the java.time package is used to obtain the current date. The DateTimeFormatter class is then used to format the date in the desired pattern. The formatted date is then assigned to a string variable d, which is printed to the console using System.out.println(). Overall, this program is useful for getting the current date in a specific format, which can be useful for various applications.

Source Code

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Local_Date
{
	public static void main(String Args[])
	{
		String d; 
		LocalDate ld = LocalDate.now();
		DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		d = formatterLocalDate.format(ld);
		System.out.println("yyyy-MM-dd = " + d);
	}
}

Output

yyyy-MM-dd = 2022-11-05

Example Programs