Write a Java Program to Get the current day number of a month using the get() method of Calendar class


This Java program retrieves and prints the current day of the month 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 get() method is then called with the Calendar.DATE constant to retrieve the day of the month. 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 DayNum_Month: This defines a public class named DayNum_Month.
  • 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("Current Day : " + cal.get(Calendar.DATE)); : This prints the string "Current Day : " followed by the value returned by the get() method of the Calendar object when called with the Calendar.DATE constant. This retrieves the day of the month, which is then printed to the console.

Source Code

import java.util.*;
public class DayNum_Month
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
		System.out.println("Current Day : " + cal.get(Calendar.DATE));
	}
}

Output

Current Day : 17

Example Programs