Gregorian calendar Java

A concrete subclass of the Calendar class is referred to as GregorianCalendar. The GregorianCalendar class has an implementation of all of its inherited members. The Calendar class implements the mostly used Gregorian calendar. In order to use the Gregorian calendar in Java, we import the Java.util.GregorianCalendar class in our program.

Gregorian calendar Java

We cannot instantiate the Calendar class because of being an abstract class. So, the initialization of the calendar is done in the following way:

The initialization of the cal object is done with the current date and time in the default locale and timezone.

We can instantiate the GregorianCalendar class because of being a concrete class. So, the initialization of the calendar is done in the following way:

The initialization of the gcal object is done with the current date and time in the default locale and timezone.

The anno Domini(AD) and the Before Christ(BC) are the two fields defined by the GregorianCalendar class.

Constructors of GregorianCalendar class

For GregorianCalendar class object, there are the following constructors:

GregorianCalendar()

In order to initialize the object with the current time in the default time zone with the default locale, GregorainCalendar() is used.

GregorianCalendar(int year, int month, int day)

In order to initialize the object with the date-set defined in the default locale and time zone, GregorianCalendar(int year, int month, int day) is used.

GregorianCalendar(int year, int month, int day, int hours, int min)

It initializes the object with the date and time-set defined in the default locale and time zone.

GregorianCalendar(int year, int month, int day int hours, int minutes, int seconds)

It initializes the object with the date and more specific time-set defined in the default locale and time zone.

GregorianCalendar(Locale locale)

It initializes the object with a defined date, time, and locale.

GregorianCalendar(TimeZone timeZone)

It initializes the object with the defined date, time-set, and TimeZone.

GregorianCalendar(TimeZone timeZone, Locale locale)

It initializes the object with the defined Locale and TimeZone.

Let's implement the code for all these constructors to understand how they actually work in Java.

In the below example, we define the instance of the GregorianCalendar by using its default constructor.

GregorianCalendarExample1.java

Output

Gregorian calendar Java

In the below example, we define the instance of the GregorianCalendar by using a defined date-set.

GregorianCalendarExample2.java

Output

Gregorian calendar Java

In the below example, we define the instance of the GregorianCalendar by using a more specific date and time set.

GregorianCalendarExample3.java

Output

Gregorian calendar Java

In the below example, we define the instance of the GregorianCalendar by using the specified time zone.

GregorianCalendarExample4.java

Output

Gregorian calendar Java

In the below example, we define the instance of the GregorianCalendar by using the specified locale.

GregorianCalendarExample5.java

Output

Gregorian calendar Java

In the below example, we define the instance of the GregorianCalendar by using the specified locale and time zone.

GregorianCalendarExample6.java

Output

Gregorian calendar Java




Latest Courses