Write a Java program to get and display information of a default calendar


This Java program uses the Calendar class to obtain the current date and time, and then prints out the values of several fields using the get() method.

The program prints out the following date and time fields:

  • Calendar.DATE: the day of the month (1-31)
  • Calendar.MONTH: the month value (0-11)
  • Calendar.YEAR: the year value
  • Calendar.HOUR: the hour value in 12-hour clock format (0-11)
  • Calendar.MINUTE: the minute value (0-59)

Note that the program uses Calendar.getInstance() method to create a new Calendar instance that represents the current date and time in the default time zone and locale. The values of the date and time fields depend on the system clock and time zone settings.

Also note that the program prints out the hour value in 12-hour clock format (Calendar.HOUR), which may not be appropriate in all contexts. To obtain the hour value in 24-hour clock format, use the Calendar.HOUR_OF_DAY field instead.

Source Code

import java.util.*;
public class Display_Default_Calendar
{
	public static void main(String[] args)
	{
		Calendar c = Calendar.getInstance();
		System.out.println("Day : " + c.get(Calendar.DATE));
		System.out.println("Month : " + c.get(Calendar.MONTH));
		System.out.println("Year : " + c.get(Calendar.YEAR));
		System.out.println("Hour : " + c.get(Calendar.HOUR));
		System.out.println("Minute : " + c.get(Calendar.MINUTE));
	}
}

Output

Day : 5
Month : 10
Year : 2022
Hour : 2
Minute : 22

Example Programs