Java: Get the maximum value of the year, month, week, date from the current date of a default calendar
Java DateTime, Calendar: Exercise-3 with Solution
Write a Java program to get the maximum value of the year, month, week, and date from the current date of a default calendar.
Sample Solution:
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:Tue Jun 20 14:10:20 IST 2017 Actual Maximum Year: 292278994 Actual Maximum Month: 11 Actual Maximum Week of Year: 52 Actual Maximum Date: 30
N.B.: The result may varry for your system date and time.
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Java program to get and display information (year, month, day, hour, minute) of a default calendar.
Next: Write a Java program to get the minimum value of year, month, week, date from the current date of a default calendar.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-exercises/datetime/java-datetime-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics