Get Last Day of The Month

Calendar class inherits from Java.util.calendar.Calendar is base abstract class, that is used for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR.

Get Last Day of The Month

     

Calendar class inherits from Java.util.calendar.Calendar is  base abstract class, that is used for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR.. A Date object  specific instant in time with millisecond.

In this Tutorial we want to describe you a code that helps you in getting the last day of the month. For this we have a class name MonthDaysExample.Inside the main method the calendar call the get Instance method  ( ) returns you the calendar object with a specific time zone and locale. we declared and  initialized a int variable year, month and date.

1)calendar.set( )  -This method set the field for year, month and date.

2)getActualMaximum( )- This method  Return the maximum value for this field, given the current date and store its value int variable max day.

The println print the Max Day  from the int variable max day of max day of month February 2009.

In the same way we print the Max day of  February 2004 in this code.

MonthDaysExample.java

import java.text.*;
import java.util.*;

public class MonthDaysExample {

  public static void main(String args[]) {

  Calendar calendar = Calendar.getInstance();

  int year = 2009;
  int month = Calendar.FEBRUARY;
  int date = 1;

  calendar.set(year, month, date);

  int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  System.out.println("Max Day: " + maxDay);

  calendar.set(2004, Calendar.FEBRUARY, 1);
  maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  System.out.println("Max Day: " + maxDay);

  }
}

Output:

Max Day: 28

Max Day: 29


Download code