GregorianCalendar getMaximum() Method in Java

The java.util.GregorianCalendar.getMaximum() method is an in-built function in Java which returns the maximum value for the particular calendar field passed as parameter to the function. For any possible time value, it is the highest value that the get() method returns, considering the current values of the getFirstDayOfWeek(), getMinimalDaysInFirstWeek(), getGregorianChange() and getTimeZone() methods as well.

Syntax:

public int getMaximum(int calendarfield)

Parameters: This function accepts one mandatory parameter calendarfield whose maximum value for this GregorianCalender instance will be returned by the function.



Return Value: This method returns the maximum value of the specified calendar field for this GregorianCalender instance.

Examples:

Input : DAY_OF_MONTH
Output : 31

Input : WEEK_OF_MONTH
Output : 6

Below programs illustrate the java.util.GregorianCalendar.getMaximum() function:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate getMaximum() function
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new calendar
        GregorianCalendar cal = (GregorianCalendar)
                     GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println("Current Date and Time : "
                                    + cal.getTime());
  
        // Display maximum for WEEK_OF_MONTH
        int maxm = cal.getMaximum
                   (GregorianCalendar.WEEK_OF_MONTH);
        System.out.println("Maximum for WEEK_OF_MONTH field :"
                                                     + maxm);
  
        // Display maximum for DAY_OF_MONTH
        maxm = cal.getMaximum
                    (GregorianCalendar.DAY_OF_MONTH);
        System.out.println("Maximum for DAY_OF_MONTH field:"
                                                    + maxm);
    }
}

chevron_right


Output:

Current Date and Time : Fri Jul 27 12:44:38 UTC 2018
Maximum for WEEK_OF_MONTH field :6
Maximum for DAY_OF_MONTH field:31

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate getMaximum() function
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Create a new calendar
        GregorianCalendar cal = (GregorianCalendar)
                          GregorianCalendar.getInstance();
  
        // Display the current date and time
        System.out.println("Current Date and Time : "
                           + cal.getTime());
  
        // Display maximum for YEAR
        int maxm = cal.getMaximum(GregorianCalendar.YEAR);
        System.out.println("Maximum for YEAR field :"
                           + maxm);
  
        // Display maximum for HOUR_OF_DAY
        maxm = cal.getMaximum(GregorianCalendar.HOUR_OF_DAY);
        System.out.println("Maximum for HOUR_OF_DAY field:"
                           + maxm);
    }
}

chevron_right


Output:

Current Date and Time : Fri Jul 27 12:44:40 UTC 2018
Maximum for YEAR field :292278994
Maximum for HOUR_OF_DAY field:23

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#getMaximum()




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.