Write a Java program to get the last date of the month


This Java program calculates and prints the last date 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 LastDate_Month - This line defines a public class named LastDate_Month.
  • 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.
  • cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); - This line sets the calendar's day of the month to the maximum value using the set() method and the getActualMaximum() method to retrieve the maximum value of the day of the month for the current month.
  • System.out.println(cal.getTime()); - This line prints the calendar's date and time using the getTime() method, which returns a Date object that can be printed using the default toString() method.

This program uses the Calendar class to set the day of the month to the last day of the current month and then prints the resulting date using the getTime() method.

Source Code

import java.util.*;
public class LastDate_Month
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		System.out.println(cal.getTime());
	}
}

Output

Wed Nov 30 14:04:50 IST 2022

Example Programs