Get Maximum Value of year, month, week, date from the Current Date in Java


This Java program uses the Calendar class to obtain the current date and time and then determines the maximum value for various date and time fields using the getActualMaximum() method.

The getActualMaximum() method returns the maximum value that can be assigned to a particular field of the Calendar instance, taking into account the current value of other fields. For example, the maximum value of the Calendar.DATE field may depend on the current month and year.

The program prints out the maximum value of the following fields:

  • Calendar.DATE: the maximum day of the month
  • Calendar.MONTH: the maximum month value (0-11)
  • Calendar.YEAR: the maximum year value
  • Calendar.WEEK_OF_YEAR: the maximum week number in the year

Note that the getActualMaximum() method returns different values depending on the time zone and locale settings of the Calendar instance. Also, the program does not prompt the user for input or perform any other meaningful computation beyond obtaining the maximum values of the date and time fields.

Source Code

import java.util.*;
public class Maximum_ValDate
{
	public static void main(String[] args)
	{
		Calendar cal = Calendar.getInstance();
		System.out.println("Current Date and Time:" + cal.getTime());	
		int maxDate = cal.getActualMaximum(Calendar.DATE);
		int maxMonth = cal.getActualMaximum(Calendar.MONTH);
		int maxYear = cal.getActualMaximum(Calendar.YEAR);
		int maxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);		
		System.out.println("Maximum Value of Date: "+maxDate);
		System.out.println("Maximum Value of Month: "+maxMonth);
		System.out.println("Maximum Value of Year: "+maxYear);
		System.out.println("Maximum Value of Week of Year: "+maxWeek);	
	}
}

Output

Current Date and Time:Sat Nov 05 14:21:16 IST 2022
Maximum Value of Date: 30
Maximum Value of Month: 11
Maximum Value of Year: 292278993
Maximum Value of Week of Year: 53

Example Programs