Write a Java program to print LocalTime and LocalDate yyyy-MM-dd HH:mm:ss


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

Source Code

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Local_DateTime
{
	public static void main(String Args[])
	{
		String dt; 
		LocalDateTime ldt = LocalDateTime.now();
		DateTimeFormatter formatterLocalDateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		dt = formatterLocalDateTime.format(ldt);
		System.out.println("yyyy-MM-dd HH:mm:ss = " + dt);
	}
}

Output

yyyy-MM-dd HH:mm:ss = 2022-11-05 12:39:21

Example Programs