Write a Java program to get the last day of the current month
This Java program calculates and prints the last day of the current month 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.
- public class LastDay_CurrentMonth - This line defines a public class named LastDay_CurrentMonth.
- public static void main(String[] args) - This line defines the main method of the program, which is the entry point for the program.
- Calendar cal = Calendar.getInstance(); - This line creates a new instance of the Calendar class using the getInstance() method.
- System.out.println(cal.getActualMaximum(Calendar.DAY_OF_MONTH)); - This line retrieves the maximum value of the day of the month for the current month using the getActualMaximum() method and the Calendar.DAY_OF_MONTH constant, which represents the day of the month field.
- System.out.println(cal.getMaximum(Calendar.DAY_OF_MONTH)); - Alternatively, this line retrieves the maximum value of the day of the month for the current month using the getMaximum() method and the Calendar.DAY_OF_MONTH constant, which also represents the day of the month field.
Source Code
import java.util.*;
public class LastDay_CurrentMonth
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
System.out.println(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
}
}
Output
30