Calendar.getActualMaximum(field) Maximum possible value of the input field .
field : field for which the maximum possible value is required, Example : DAY_OF_MONTH
Returns the maximum value .
Sample script with output is here. Note that we used February month and year 2020, this is a leap year. Hence we are getting 29 as maximum vlaue for DAY_OF_MONTH and DAY_OF_YEAR returning 366.
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal = Calendar.getInstance();// created calendar
my_cal.set(2020, 1, 25); // Year, month, date
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_WEEK));//7
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_MONTH));//29
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_YEAR));//366
}
}
We can try the same code by changing the set date here. The output for DAY_OF_MONTH and DAY_OF_YEAR will change. We used 25th March of 2021.
package my_proj;
import java.util.Calendar;
public class my_first {
public static void main(String[] args) {
Calendar my_cal = Calendar.getInstance();// created calendar
my_cal.set(2021, 2, 25); // Year, month, date
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_WEEK));//7
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_MONTH));//31
System.out.println(my_cal.getActualMaximum(Calendar.DAY_OF_YEAR));//365
}
}
By comparing outputs of above two examples you can see the output changes for some fields like DAY_OF_YEAR, DAY_OF_MONTH etc but not changing for DAY_OF_WEEK when we use different Calendar objects.
Year 2021 is not a leap year and hence February month has 29 days and number of days in a year is 366 ( not 365).
Here is the code to get YEAR and MONTH ( with output )