Write a Java program to print LocalTime HH:mm:ss


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

Source Code

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
class Local_Time
{
	public static void main(String Args[])
	{
		String t;
		LocalTime lt = LocalTime.now();
		DateTimeFormatter formatterLocalTime = DateTimeFormatter.ofPattern("HH:mm:ss");
		t = formatterLocalTime.format(lt);
		System.out.println("HH:mm:ss = " + t);
	}
}

Output

HH:mm:ss = 12:38:04

Example Programs