Get Minimum Value of year, month, week, date from the Current Date in Java
This Java program uses the Calendar class to find the minimum values for various fields like date, month, year and week of the year. Here are the key points of the program:
- import java.util.*; - This line imports all classes from the java.util package, which includes the Calendar class.
- public class Minimum_ValDate - This line defines a class named Minimum_ValDate.
- public static void main(String[] args) - This line defines the main method of the program, which is the entry point for the program.
- Calendar cal = Calendar.getInstance(); - This line creates a new instance of the Calendar class using the getInstance() method.
- System.out.println("Current Date and Time : " + cal.getTime()); - This line prints the current date and time using the getTime() method of the Calendar class.
- int maxDate = cal.getActualMinimum(Calendar.DATE); - This line gets the minimum value for the date field using the getActualMinimum() method of the Calendar class and assigns it to the maxDate variable.
- int maxMonth = cal.getActualMinimum(Calendar.MONTH); - This line gets the minimum value for the month field using the getActualMinimum() method of the Calendar class and assigns it to the maxMonth variable.
- int maxYear = cal.getActualMinimum(Calendar.YEAR); - This line gets the minimum value for the year field using the getActualMinimum() method of the Calendar class and assigns it to the maxYear variable.
- int maxWeek = cal.getActualMinimum(Calendar.WEEK_OF_YEAR); - This line gets the minimum value for the week of the year field using the getActualMinimum() method of the Calendar class and assigns it to the maxWeek variable.
- System.out.println("Minimum Value of Date : "+maxDate); - This line prints the minimum value for the date field.
- System.out.println("Minimum Value of Month : "+maxMonth); - This line prints the minimum value for the month field.
- System.out.println("Minimum Value of Year : "+maxYear); - This line prints the minimum value for the year field.
- System.out.println("Minimum Value of Week of Year : "+maxWeek); - This line prints the minimum value for the week of the year field.
Source Code
import java.util.*;
public class Minimum_ValDate
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
System.out.println("Current Date and Time : " + cal.getTime());
int maxDate = cal.getActualMinimum(Calendar.DATE);
int maxMonth = cal.getActualMinimum(Calendar.MONTH);
int maxYear = cal.getActualMinimum(Calendar.YEAR);
int maxWeek = cal.getActualMinimum(Calendar.WEEK_OF_YEAR);
System.out.println("Minimum Value of Date : "+maxDate);
System.out.println("Minimum Value of Month : "+maxMonth);
System.out.println("Minimum Value of Year : "+maxYear);
System.out.println("Minimum Value of Week of Year : "+maxWeek);
}
}
Output
Current Date and Time : Sat Nov 05 14:20:09 IST 2022
Minimum Value of Date : 1
Minimum Value of Month : 0
Minimum Value of Year : 1
Minimum Value of Week of Year : 1