class GregorianCalendar

February 23, 2011 | java.util

GregorianCalendar is a concrete class, a subclass of abstract class Calendar. The GregorianCalendar class represents the calendar we use everyday. It is used to manipulate date and retrieve date particulars. This was introduced with JDK 1.1.

Following statement gives the class signature

public class GregorianCalendar extends Calendar

Following program illustrates the usage of a few variables and methods of GregorianCalendar.

import java.util.*;  // for Calendar and GregorianCalendar
public class GCInfo
{
  public static void main( String agrs[])
  {
    GregorianCalendar cal = new GregorianCalendar( );
    int currentyear = cal.get(Calendar.YEAR);
    System.out.println("Year info: " + currentyear);
    System.out.println("Era info: " + cal.get(Calendar.ERA));
    System.out.println("Leap year: " +  cal.isLeapYear(currentyear));
    System.out.println("Hour info: " + cal.get(Calendar.HOUR));
    System.out.println("Minute info: " + cal.get(Calendar.MINUTE));
    System.out.println("Second info: " + cal.get(Calendar.SECOND));
    System.out.println("Millisecond info: " + cal.get(Calendar.MILLISECOND));
    System.out.println("Month info: " + cal.get(Calendar.MONTH));
    System.out.println("Week info of month: " + cal.get( Calendar.WEEK_OF_MONTH));
    System.out.println("Week in the year: " + cal.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Date info: " + cal.get(Calendar.DATE));
    System.out.println("Day of month info: " + cal.get(Calendar.DAY_OF_MONTH));
    System.out.println("Day of year info: " + cal.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of week info: " + cal.get(Calendar.DAY_OF_WEEK));
    System.out.println("Day of week in the month: " + cal.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("AM or PM info: " + cal.get(Calendar.AM_PM));
    System.out.println("Hour of the day info: " + cal.get(Calendar.HOUR_OF_DAY));
    
    if(cal.get(Calendar.AM_PM) == Calendar.PM)
         System.out.println("Good Evening");
    else
         System.out.println("Good Morning");

    GregorianCalendar now = new GregorianCalendar();
    GregorianCalendar future = new GregorianCalendar(2020, 5, 25);
    System.out.println(now);
    System.out.println(future);
  }
}

All the methods and variables are self-descriptive. The first month of the year (January) is given as zero.