Get the last day of month in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 10:19:01
Advertisements