Java util GregorianCalendar class

java.util.GregorianCalendar

GregorianCalendar is a concrete subclass of Calendar and provides the most commonly used standard calendar system. The getInstance() method of Calendar class is used to get the GregorianCalendar object initialized with the current date and time in the default locale and time zone. We can also use new operator with GregorianCalendar class constructor.

Calendar calendar = Calendar.getInstance();
or
Calendar calendar = new GregorianCalendar();

Note: Gregorian calendar is the only implementation provided for Calendar class in java.

GregorianCalendar class declaration

public class GregorianCalendar extends Calendar

java.util.GregorianCalendar class example

package com.w3spoint;
 
import java.util.Calendar;
import java.util.GregorianCalendar;
 
public class GregorianCalenderTest {
	public static void main(String args[]){
	   GregorianCalendar calendar = new GregorianCalendar();  
	   System.out.println("The current date is : " + calendar.getTime());  
	   calendar.add(Calendar.DATE, -10);  
	   System.out.println("10 days ago: " + calendar.getTime());  
	   calendar.add(Calendar.MONTH, 3);  
	   System.out.println("3 months later: " + calendar.getTime());  
	   calendar.add(Calendar.YEAR, 4);  
	   System.out.println("4 years later: " + calendar.getTime()); 
	   int maximum = calendar.getMaximum(Calendar.DAY_OF_WEEK);  
	   System.out.println("Maximum number of days in week: " + maximum);  
	   maximum = calendar.getMaximum(Calendar.WEEK_OF_YEAR);  
	   System.out.println("Maximum number of weeks in year: " + maximum);  
	   int minimum = calendar.getMinimum(Calendar.DAY_OF_WEEK);  
	   System.out.println("Minimum number of days in week: " + minimum);  
	   minimum = calendar.getMinimum(Calendar.WEEK_OF_YEAR);  
	   System.out.println("Minimum number of weeks in year: " + minimum);  
	   //Test if the current year is a leap year
	   if(calendar.isLeapYear(calendar.get(Calendar.YEAR))) {
	       System.out.println("The current year is a leap year");
	   }else {
	       System.out.println("The current year is not a leap year");
	   }
	}
}

Output:

The current date is : Mon Apr 09 19:36:11 IST 2018
10 days ago: Fri Mar 30 19:36:11 IST 2018
3 months later: Sat Jun 30 19:36:11 IST 2018
4 years later: Thu Jun 30 19:36:11 IST 2022
Maximum number of days in week: 7
Maximum number of weeks in year: 53
Minimum number of days in week: 1
Minimum number of weeks in year: 1
The current year is not a leap year
Content Protection by DMCA.com
Please Share