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:
// 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); } } |
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:
// 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); } } |
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()