Write a Java Program to Print the current date-time using Calendar class


This Java program retrieves and prints the current date and time using the Calendar class. The getInstance() method of the Calendar class is called to obtain a Calendar object that represents the current date and time. The getTime() method is then called to retrieve the current date and time as a Date object. Here's an explanation of the code:

  • import java.util.*;: This imports the java.util package, which contains the Calendar class and other utility classes.
  • public class DateTime_Calendar: This defines a public class named DateTime_Calendar.
  • public static void main(String[] args): This is the entry point of the program. It declares a public static method named main that takes an array of strings as an argument.
  • Calendar cal = Calendar.getInstance();: This creates a new Calendar object representing the current date and time.
  • System.out.println("The Current DateTime is : " + cal.getTime());: This prints the string "The Current DateTime is : " followed by the current date and time as returned by the getTime() method of the Calendar object. This date and time is represented as a Date object, which is then printed to the console.

Source Code

import java.util.*;
public class DateTime_Calendar
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
		System.out.println("The Current DateTime is : " + cal.getTime());
	}
}

Output

The Current DateTime is : Sat Dec 17 14:30:53 IST 2022

Example Programs