Menu
Topics Index
...
`


More Utility Classes >
Siva Nookala - 20 Feb 2017
GregorianCalendar is a concrete subclass of Calendar In Java - java.util.Calendar Class and provides the standard calendar system used by most of the world. The getInstance() method of Calendar returns a GregorianCalendar initialized with the current date and time in the default locale and time zone. GregorianCalendar defines two fields: AD and BC. These represent the two eras defined by the Gregorian calendar.

GregorianCalendar Constructors:
ConstructorDescription
GregorianCalendar()Constructs a default GregorianCalendar using the current time in the default time zone with the default locale.
GregorianCalendar(int year,int month,int dayOfMonth)Constructs a GregorianCalendar with the given date set in the default time zone with the default locale.
GregorianCalendar(int year,int month,int dayOfMonth,int hourOfDay,int minute)Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.
GregorianCalendar(int year,int month,int dayOfMonth,int hourOfDay,int minute,int second)Constructs a GregorianCalendar with the given date and time set for the default time zone with the default locale.
GregorianCalendar(Locale aLocale)Constructs a GregorianCalendar based on the current time in the default time zone with the given locale.
GregorianCalendar(TimeZone zone)Constructs a GregorianCalendar based on the current time in the given time zone with the default locale.
GregorianCalendar(TimeZone zone,Locale aLocale)Constructs a GregorianCalendar based on the current time in the given time zone with the given locale.

GregorianCalendar Methods:
MethodDescription
void add(int field,int amount)Adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules.
Object clone()Creates and returns a copy of this object.
protected void computeFields()Converts the time value (millisecond offset from the Epoch) to calendar field values.
protected void computeTime()Converts calendar field values to the time value (millisecond offset from the Epoch).
boolean equals(Object obj)Compares this GregorianCalendar to the specified Object.
int getActualMaximum(int field)Returns the maximum value that this calendar field could have, taking into consideration the given time value and the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange and getTimeZone methods.
int getActualMinimum(int field)Returns the minimum value that this calendar field could have, taking into consideration the given time value and the current values of the getFirstDayOfWeek, getMinimalDaysInFirstWeek, getGregorianChange and getTimeZone methods.
int getGreatestMinimum(int field)Returns the highest minimum value for the given calendar field of this GregorianCalendar instance.
Date getGregorianChange()Gets the Gregorian Calendar change date.
int getLeastMaximum(int field)Returns the lowest maximum value for the given calendar field of this GregorianCalendar instance.
int getMaximum(int field)Returns the maximum value for the given calendar field of this GregorianCalendar instance.
int getMinimum(int field)Returns the minimum value for the given calendar field of this GregorianCalendar instance.
TimeZone getTimeZone()Gets the time zone.
int getWeeksInWeekYear()Returns the number of weeks in the week year represented by this GregorianCalendar.
int getWeekYear()Returns the week year represented by this GregorianCalendar.
int hashCode()Generates the hash code for this GregorianCalendar object.
boolean isLeapYear(int year)Determines if the given year is a leap year.
isWeekDateSupported()Returns true indicating this GregorianCalendar supports week dates.
void roll(int field, boolean up)Adds or subtracts (up/down) a single unit of time on the given time field without changing larger fields.
void roll(int field, int amount)Adds a signed amount to the specified calendar field without changing larger fields.
void setGregorianChange(Date date)Sets the GregorianCalendar change date.
setTimeZone(TimeZone zone)Sets the time zone with the given time zone value.

Gregorian Calendar
import java.util.Calendar;
import java.util.GregorianCalendar;

class GregorianCalendarTest
{
    public static void main(String arg[])
    {
        GregorianCalendar gc = new GregorianCalendar();
        System.out.println("The current date is : " + gc.getTime());
                
        System.out.println("Month : " + gc.get(Calendar.MONTH));
        System.out.println("Date : " + gc.get(Calendar.DATE));
        System.out.println("Year : " + gc.get(Calendar.YEAR));
        
        System.out.println("Hour : " + gc.get(Calendar.HOUR));
        System.out.println("Hour of day : " + gc.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute : " + gc.get(Calendar.MINUTE));
        System.out.println("Second : " + gc.get(Calendar.SECOND));
        System.out.println("Milli second : " + gc.get(Calendar.MILLISECOND));
        
        System.out.println("Day of month : " + gc.get(Calendar.DAY_OF_MONTH));
        System.out.println("Day of week : " + gc.get(Calendar.DAY_OF_WEEK));
        System.out.println("Week of year : " + gc.get(Calendar.WEEK_OF_YEAR));
        System.out.println("Week of month : " + gc.get(Calendar.WEEK_OF_MONTH));    
    }
}
OUTPUT

The current date is : Sun Jan 19 12:24:02 IST 2014
Month : 0
Date : 19
Year : 2014
Hour : 0
Hour of day : 12
Minute : 24
Second : 2
Milli second : 295
Day of month : 19
Day of week : 1
Week of year : 4
Week of month : 4

DESCRIPTION

In this program, we are creating a date using GregorianCalendar and extracting individual month, date, year, hour, hour of the day, minute, second, milli second, day of month, day of week, week of year and week of month. Note the output will be different when you run this program.


Gregorian Calendar Methods
import java.util.*;

class GregorianCalendarMethodsTest1
{
    public static void main(String arg[])
    {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        System.out.println("The current date : " + gc.getTime());
        
        gc.add(Calendar.MONTH, 3); // Adds 3 months // LINE A
        System.out.println("Adds 3 months to present year : " + gc.getTime());
        
        GregorianCalendar gc1 = (GregorianCalendar) gc.clone(); // Cloning method
        System.out.println("Clone date : " + gc1.getTime());
        
        if (gc.equals(gc1)) { // Equals method
            System.out.println("Both calendars are same");
        }
        
        System.out.println("The 2016 is Leap Year : " + gc.isLeapYear(2016)); // Leap year
        
        gc.roll(Calendar.DAY_OF_MONTH, true); // Rolls a single day
        System.out.println("Adds a single day : " + gc.getTime());
        
        gc.roll(Calendar.DAY_OF_MONTH, 5); // Rolls 5 days
        System.out.println("Adds five days : " + gc.getTime());
        
        gc.set(Calendar.MONTH, 11); // Sets month as December    
    }
}
OUTPUT

The current date : Sun Jan 19 12:58:07 IST 2014
Adds 3 months to present year : Sat Apr 19 12:58:07 IST 2014
Clone date : Sat Apr 19 12:58:07 IST 2014
Both calendars are same
The 2016 is Leap Year : true
Adds a single day : Sun Apr 20 12:58:07 IST 2014
Adds five days : Fri Apr 25 12:58:07 IST 2014

DESCRIPTION

In this program, The getInstance method takes the current date in gc object. The add, clone, equals, isLeapYear, and  roll methods are used.

THINGS TO TRY
  • At LINE A, add method, replace -2 in place of 3 and see the output difference.
  • In isLeapYear method replace 2015 in place of 2016 and see the output difference.
  • In roll method replace false in place of true and see the output difference.
  • In roll method replace Calendar.MONTH in place of Calendar.DAY_OF_MONTH and see the output difference.

Gregorian Calendar Methods127
import java.util.*;

class GregorianCalendarMethodsTest2
{
    public static void main(String arg[])
    {
        GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
        System.out.println("The current date : " + gc.getTime());
        
        int max = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Actual Maximum : " + max);
        
        int min = gc.getActualMinimum(Calendar.DAY_OF_MONTH);
        System.out.println("Actual Minimum : " + min);
        
        min = gc.getGreatestMinimum(Calendar.DAY_OF_MONTH);
        System.out.println("Greatest Minimum : " + min);
        
        max = gc.getLeastMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Least Maximum : " + max);
        
        max = gc.getMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("Maximum : " + max);
        
        min = gc.getMinimum(Calendar.DAY_OF_MONTH);
        System.out.println("Minimum : " + min);
        
        System.out.println("Time Zone : " + gc.getTimeZone().getDisplayName());
        
        System.out.println("The hash code : " + gc.hashCode());
        
        System.out.println("Get Gregorian change date : " + gc.getGregorianChange());
        
        gc.setGregorianChange(new Date(2016, 7, 10, 05, 48, 32));
        System.out.println("Gregorian Changed Date : " + gc.getGregorianChange());    
    }
}
OUTPUT

The current date : Sun Jan 19 13:11:52 IST 2014
Actual Maximum : 31
Actual Minimum : 1
Greatest Minimum : 1
Least Maximum : 28
Maximum : 31
Minimum : 1
Time Zone : India Standard Time
The hash code : -1527345468
Get Gregorian change : Fri Oct 15 05:30:00 IST 1582
Gregorian Change Date : Thu Aug 10 05:48:32 IST 3916

DESCRIPTION

In this program, the getInstance method takes the current date in gc object and getting the actual maximum, actual minimum, greatest minimum, least maximum, maximum, minimum day of month. We are also displaying time zone, hash code, getting gregorian change date and set gregorian change date.

THINGS TO TRY
  • In getActualMaximum, replace DAY_OF_MONTH by MONTH and see the output difference.
  • In getMaximum, replace DAY_OF_MONTH by DAY_OF_YEAR and see the output difference.
  • In setGregorianChange, give your own date and time and see the output.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.