w3resource logo


Java Programming Exercies

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

Secondary Nav

Java DateTime, Calendar : Exercise-4 with Solution

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

Java Code:

import java.util.*;
public class Exercise4 {
 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.getActualMinimum(Calendar.YEAR);
		int actualMaxMonth = cal.getActualMinimum(Calendar.MONTH);
		int actualMaxWeek = cal.getActualMinimum(Calendar.WEEK_OF_YEAR);
		int actualMaxDate = cal.getActualMinimum(Calendar.DATE);
		
		System.out.println("Actual Minimum Year: "+actualMaxYear);
		System.out.println("Actual Minimum Month: "+actualMaxMonth);
		System.out.println("Actual Minimum Week of Year: "+actualMaxWeek);
		System.out.println("Actual Minimum Date: "+actualMaxDate+"\n");
		System.out.println();		
	  }
}

Sample Output :

Current Date and Time:Thu Sep 15 13:31:52 IST 2016                               
Actual Minimum Year: 1                                                           
Actual Minimum Month: 0                                                          
Actual Minimum Week of Year: 1                                                   
Actual Minimum Date: 1 

Java Practice online:

import java.util.*;
public class Exercise4 {
 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.getActualMinimum(Calendar.YEAR);
		int actualMaxMonth = cal.getActualMinimum(Calendar.MONTH);
		int actualMaxWeek = cal.getActualMinimum(Calendar.WEEK_OF_YEAR);
		int actualMaxDate = cal.getActualMinimum(Calendar.DATE);
		
		System.out.println("Actual Minimum Year: "+actualMaxYear);
		System.out.println("Actual Minimum Month: "+actualMaxMonth);
		System.out.println("Actual Minimum Week of Year: "+actualMaxWeek);
		System.out.println("Actual Minimum Date: "+actualMaxDate+"\n");
		System.out.println();		
	  }
}



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

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables