Java.util.GregorianCalendar Class in Java
Last Updated :
30 Nov, 2021
Prerequisites : java.util.Locale, java.util.TimeZone, Calendar.get()
GregorianCalendar is a concrete subclass(one which has implementation of all of its inherited members either from interface or abstract class) of a Calendar that implements the most widely used Gregorian Calendar with which we are familiar.
java.util.GregorianCalendar vs java.util.Calendar
The major difference between GregorianCalendar and Calendar classes are that the Calendar Class being an abstract class cannot be instantiated. So an object of the Calendar Class is initialized as:
Calendar cal = Calendar.getInstance();
Here, an object named cal of Calendar Class is initialized with the current date and time in the default locale and timezone. Whereas, GregorianCalendar Class being a concrete class, can be instantiated. So an object of the GregorianCalendar Class is initialized as:
GregorianCalendar gcal = new GregorianCalendar();
Here, an object named gcal of GregorianCalendar Class is initialized with the current date and time in the default locale and timezone.
Fields defined :
GregorianCalendar Class defines two fields:
AD : referring to the common era(anno Domini)
BC : referring to before common era(Before Christ)
Constructors : There are several constructors for GregorianCalendar objects. Broadly classifying, constructors for GregorianCalendar either initialize the object with the user specified date and/or time in the default locale and time zone, or initialize the object with default date and time in the user specified locale and/or time zone. These are as follows:
Constructor Signature |
Description |
GregorianCalendar() |
initializes the object with the current date and time in the default locale and time zone |
GregorianCalendar(int year, int month, int dayOfMonth) |
initializes the object with the date-set passed as parameters in the default locale and time zone |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) |
initializes the object with the date and time-set passed as parameters in the default locale and time zone |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) |
initializes the object with the date and more specific time-set passed as parameters in the default locale and time zone |
GregorianCalendar(Locale locale) |
initializes the object with the current date and time in the default time zone and the locale passed as parameters |
GregorianCalendar(TimeZone timeZone) |
initializes the object with the current date and time in the default locale and the time zone passed as parameters |
GregorianCalendar(TimeZone timeZone, Locale locale) |
initializes the object with the current date and time in the locale and the time zone passed as parameters |
Methods from(), toZonedDateTime(), getCalendarType() were introduced in JDK 8.
Java
import java.util.Calendar;
import java.util.GregorianCalendar;
class CalendarGFG {
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
GregorianCalendar gcal = new GregorianCalendar();
System.out.println( "Calendar date: "
+ cal.getTime());
System.out.print( "Gregorian date: "
+ gcal.getTime());
}
}
|
Output:
Calendar date: Sat Apr 28 13:36:37 UTC 2018
Gregorian date: Sat Apr 28 13:36:37 UTC 2018
Example to demonstrate the usage of various constructors:
1. Using default constructor
Java
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
GregorianCalendar gcal = new GregorianCalendar();
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
}
}
|
Output:
Date: Apr 30, 2018
Time: 10:21:51 PM
Time Zone: Coordinated Universal Time
Locale: English (United States)
2. By passing year, month, dayOfMonth as parameters:
Java
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
GregorianCalendar gcal = new GregorianCalendar( 2018 , 3 , 30 );
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
}
}
|
Output:
Date: Apr 30, 2018
Time: 0:0:0 AM
Time Zone: Coordinated Universal Time
Locale: English (United States)
3. By passing year, month, dayOfMonth, hourOfDay, minute:
Java
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
GregorianCalendar gcal = new GregorianCalendar( 2018 , 3 , 30 , 10 , 21 );
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
}
}
|
Output:
Date: Apr 30, 2018
Time: 10:21:0 AM
Time Zone: Coordinated Universal Time
Locale: English (United States)
4. By passing year, month, dayOfMonth, hourOfDay, minute, second:
Java
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarGFG {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
GregorianCalendar gcal = new GregorianCalendar( 2018 , 3 , 30 , 10 , 21 , 51 );
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: "
+ Locale.getDefault().getDisplayName());
}
}
|
Output:
Date: Apr 30, 2018
Time: 10:21:51 AM
Time Zone: Coordinated Universal Time
Locale: English (United States)
5. By passing timeZone as parameter:
Java
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
TimeZone tz = TimeZone.getTimeZone( "GMT+5:30" );
GregorianCalendar gcal = new GregorianCalendar(tz);
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: " + gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + Locale.getDefault().getDisplayCountry());
}
}
|
Output:
Date: May 1, 2018
Time: 4:24:7 AM
Time Zone: GMT+05:30
Locale: United States
6. By passing the locale as a parameter:
Java
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
Locale l = new Locale( "en" , "IN" );
GregorianCalendar gcal = new GregorianCalendar(l);
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: "
+ gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + l.getDisplayCountry());
}
}
|
Output:
Date: Apr 30, 2018
Time: 10:58:30 PM
Time Zone: Coordinated Universal Time
Locale: India
7. By passing timeZone and locale as parameters:
Java
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarTest {
public static void main(String args[])
{
String month[] = { "Jan" , "Feb" , "Mar" , "Apr" ,
"May" , "Jun" , "Jul" , "Aug" ,
"Sep" , "Oct" , "Nov" , "Dec" };
String amPm[] = { "AM" , "PM" };
TimeZone tz = TimeZone.getTimeZone( "GMT+5:30" );
Locale l = new Locale( "en" , "IN" );
GregorianCalendar gcal = new GregorianCalendar(tz, l);
System.out.print( "Date: "
+ month[gcal.get(Calendar.MONTH)] + " "
+ gcal.get(Calendar.DATE) + ", "
+ gcal.get(Calendar.YEAR) + "\n"
+ "Time: "
+ gcal.get(Calendar.HOUR) + ":"
+ gcal.get(Calendar.MINUTE) + ":"
+ gcal.get(Calendar.SECOND) + " "
+ amPm[gcal.get(Calendar.AM_PM)] + "\n"
+ "Time Zone: "
+ gcal.getTimeZone().getDisplayName()
+ "\n"
+ "Locale: " + l.getDisplayCountry());
}
}
|
Output:
Date: May 1, 2018
Time: 4:34:59 AM
Time Zone: GMT+05:30
Locale: India
Reference:
GregorianCalendar (Java Platform SE 8 ) – Oracle Help Center
Share your thoughts in the comments
Please Login to comment...