Java GregorianCalendar Initialize

By Arvind Rai, September 23, 2021
On this page we will learn to initialize Java GregorianCalendar.
1. The java.util.GregorianCalendar is the subclass of java.util.Calendar abstract class.
2. The GregorianCalendar provides the standard calendar system.
3. The GregorianCalendar is a hybrid calendar that supports both the Julian and Gregorian calendar systems.

Here on this page, we will learn the ways to initialize the GregorianCalendar. The GregorianCalendar has different constructors to initialize it. Let discuss them one-by-one.

Constructor with No Arguments

GregorianCalendar() 
Constructs the default GregorianCalendar using the current time in the default time zone with default FORMAT locale.

Example:
Calendar c = new GregorianCalendar();
System.out.println(c.getTime()); 
Output
Wed Sep 22 18:37:20 IST 2021 

Using Date Sets

Find the constructors with date sets.
GregorianCalendar(int year, int month, int dayOfMonth)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute)
GregorianCalendar(int year, int month, int dayOfMonth, int hourOfDay, int minute, int second) 
year: Sets YEAR calendar field.
month: Sets MONTH calendar field. It starts with 0 and that is January.
dayOfMonth: Sets DAY_OF_MONTH calendar field.
hourOfDay: Sets HOUR_OF_DAY calendar field.
minute: Sets MINUTE calendar field.
second: Sets SECOND calendar field.

Example:
Calendar c1 = new GregorianCalendar(2021, 8, 22); // 8 is for September
System.out.println(c1.getTime());
Calendar c2 = new GregorianCalendar(2021, 8, 22, 10, 7);
System.out.println(c2.getTime());	
Calendar c3 = new GregorianCalendar(2021, 8, 22, 10, 7, 50);
System.out.println(c3.getTime()); 
Output
Wed Sep 22 00:00:00 IST 2021
Wed Sep 22 10:07:00 IST 2021
Wed Sep 22 10:07:50 IST 2021 

Using Locale

GregorianCalendar(Locale aLocale) 
Constructs a GregorianCalendar based on the current time in the default time zone with the given locale.
Example:
Calendar c = new GregorianCalendar(Locale.FRANCE);
System.out.println(c.getTime()); 
Output
Thu Sep 23 08:54:52 IST 2021 

Using TimeZone

GregorianCalendar(TimeZone zone) 
Constructs a GregorianCalendar based on the current time in the given time zone with the default FORMAT locale.
Example:
TimeZone t = SimpleTimeZone.getTimeZone("Asia/Calcutta"); 
Calendar c = new GregorianCalendar(t);
System.out.println(c.getTime()); 
Output
Thu Sep 23 09:19:08 IST 2021 

Using TimeZone and Locale

GregorianCalendar(TimeZone zone, Locale aLocale) 
Constructs a GregorianCalendar based on the current time in the given time zone with the given locale.
Example:
TimeZone t = SimpleTimeZone.getTimeZone("Europe/Paris"); 
Calendar c = new GregorianCalendar(t, Locale.FRANCE);
System.out.println(c.getTime()); 
Output
Thu Sep 23 09:24:32 IST 2021 

From ZonedDateTime

Find the static from method of GregorianCalendar.
static GregorianCalendar from(ZonedDateTime zdt) 
1. Obtains an instance of GregorianCalendar with the default locale from a ZonedDateTime object.
2. Since ZonedDateTime uses ISO calendar system and does not support a Julian-Gregorian cutover date, the instance of GregorianCalendar obtained from from method, is a pure Gregorian calendar and uses ISO 8601 standard for week definitions.
3. It has MONDAY as the FirstDayOfWeek and 4 as the value of the MinimalDaysInFirstWeek.
4. If the zoned date-time is too large to represent as a GregorianCalendar, it will throw IllegalArgumentException.
Example:
ZonedDateTime zdt = ZonedDateTime.now();
GregorianCalendar gc = GregorianCalendar.from(zdt);
System.out.println(gc.getTime()); 
Output
Thu Sep 23 18:17:20 IST 2021 

Default Fields Values

The Calendar.clear() method sets all calendar field values and time values of this calendar undefined. The GregorianCalendar uses default values for some of those fields that are as following.
ERA = AD
YEAR = 1970
MONTH = JANUARY
DAY_OF_MONTH = 1
DAY_OF_WEEK = the first day of week
WEEK_OF_MONTH = 0
DAY_OF_WEEK_IN_MONTH = 1
AM_PM = AM
HOUR, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND = 0 
The GregorianCalendar does not provide default values for other fields which are not listed above.
Example:
Calendar c = new GregorianCalendar();
System.out.println(c.getTime());	
c.clear();
System.out.println(c.getTime()); 
Output
Wed Sep 22 19:54:47 IST 2021
Thu Jan 01 00:00:00 IST 1970 

Reference

Class GregorianCalendar
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us