Write a Java program to get current full date and time


This Java program prints the current full date and time using the Calendar class. Here are the key points of the program:

  • import java.util.*; - This line imports all classes from the java.util package, which includes the Calendar class.
  • class CurrentFull_DateTime - This line defines a class named CurrentFull_DateTime.
  • public static void main(String[] args) - This line defines the main method of the program, which is the entry point for the program.
  • Calendar cdt = Calendar.getInstance(); - This line creates a new instance of the Calendar class using the getInstance() method.
  • System.out.println("Current Full Date and Time : " + cdt.get(Calendar.DATE) + "-" + (cdt.get(Calendar.MONTH)+ 1) + "-" + cdt.get(Calendar.YEAR) + " "+ cdt.get(Calendar.HOUR_OF_DAY) + ":" + cdt.get(Calendar.MINUTE) + ":"+ cdt.get(Calendar.SECOND) + "." + cdt.get(Calendar.MILLISECOND)); - This line prints the current full date and time using the println() method and string concatenation. The various fields of the Calendar class are retrieved using their respective constants, such as Calendar.DATE, Calendar.MONTH, Calendar.YEAR, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, and Calendar.MILLISECOND. Note that the month field starts at 0, so 1 is added to it to get the actual month number.

Source Code

import java.util.*;
class CurrentFull_DateTime
{
	public static void main(String[] args)
	{
		Calendar cdt = Calendar.getInstance();
		System.out.println("Current Full Date and Time : " + cdt.get(Calendar.DATE) + "-"	+ (cdt.get(Calendar.MONTH)+ 1) + "-" + cdt.get(Calendar.YEAR) + " "+ cdt.get(Calendar.HOUR_OF_DAY) + ":" + cdt.get(Calendar.MINUTE) + ":"+ cdt.get(Calendar.SECOND) + "." + cdt.get(Calendar.MILLISECOND));
	}
}

Output

Current Full Date and Time : 5-11-2022 14:18:21.777

Example Programs