Get the last day of month in Java


The getActualMaximum() method returns the maximum value that the specified calendar field could have, based on the time value of this Calendar.

Import the following package to work with Calendar class in Java,

import java.util.Calendar;

Create a calendar object.

Calendar cal = Calendar.getInstance();

Use the getActualMaximum() method to get the last day of the month.

int res = cal.getActualMaximum(Calendar.DATE);

The following is an example.

Example

 Live Demo

import java.util.Calendar;
public class Main {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      int res = cal.getActualMaximum(Calendar.DATE);
      System.out.println("Today's Date = " + cal.getTime());
      System.out.println("Last Date of the current month = " + res);
   }
}

Output

Today's Date = Mon Nov 19 13:48:18 UTC 2018
Last Date of the current month = 30

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements