w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Get the maximum value of the year, month, week, date from the current date of a default calendar

Secondary Nav

Java DateTime, Calendar : Exercise-3 with Solution

Write a Java program to get the maximum value of the year, month, week, date from the current date of a default calendar.

Java Code:

import java.util.*;
public class Exercise3 {
public static void main(String[] args)
    {
     // Create a default calendar
        Calendar cal = Calendar.getInstance();
		System.out.println();
		System.out.println("\nCurrent Date and Time:" + cal.getTime());		
		int actualMaxYear = cal.getActualMaximum(Calendar.YEAR);
		int actualMaxMonth = cal.getActualMaximum(Calendar.MONTH);
		int actualMaxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
		int actualMaxDate = cal.getActualMaximum(Calendar.DATE);
		
		System.out.println("Actual Maximum Year: "+actualMaxYear);
		System.out.println("Actual Maximum Month: "+actualMaxMonth);
		System.out.println("Actual Maximum Week of Year: "+actualMaxWeek);
		System.out.println("Actual Maximum Date: "+actualMaxDate+"\n");
		System.out.println();
		
	  }
}

Sample Output :

Current Date and Time:Thu Sep 15 13:19:36 IST 2016                               
Actual Maximum Year: 292278993                                                   
Actual Maximum Month: 11                                                         
Actual Maximum Week of Year: 53                                                  
Actual Maximum Date: 30  

Java Practice online:

import java.util.*;
public class Exercise3 {
public static void main(String[] args)
    {
     // Create a default calendar
        Calendar cal = Calendar.getInstance();
		System.out.println();
		System.out.println("\nCurrent Date and Time:" + cal.getTime());		
		int actualMaxYear = cal.getActualMaximum(Calendar.YEAR);
		int actualMaxMonth = cal.getActualMaximum(Calendar.MONTH);
		int actualMaxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);
		int actualMaxDate = cal.getActualMaximum(Calendar.DATE);
		
		System.out.println("Actual Maximum Year: "+actualMaxYear);
		System.out.println("Actual Maximum Month: "+actualMaxMonth);
		System.out.println("Actual Maximum Week of Year: "+actualMaxWeek);
		System.out.println("Actual Maximum Date: "+actualMaxDate+"\n");
		System.out.println();
		
	  }
}



Join our Question Answer community to learn and share your programming knowledge.