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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "Current Date and Time : "
+ cal.getTime());
int maxm = cal.getMaximum
(GregorianCalendar.WEEK_OF_MONTH);
System.out.println( "Maximum for WEEK_OF_MONTH field :"
+ maxm);
maxm = cal.getMaximum
(GregorianCalendar.DAY_OF_MONTH);
System.out.println( "Maximum for DAY_OF_MONTH field:"
+ maxm);
}
}
|
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:
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
System.out.println( "Current Date and Time : "
+ cal.getTime());
int maxm = cal.getMaximum(GregorianCalendar.YEAR);
System.out.println( "Maximum for YEAR field :"
+ maxm);
maxm = cal.getMaximum(GregorianCalendar.HOUR_OF_DAY);
System.out.println( "Maximum for HOUR_OF_DAY field:"
+ maxm);
}
}
|
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()