Gregorian Calendar Java Current Date
GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create relevant and consistent results for all years. Dates determined using the Gregorian calendar, on the other hand, are historically accurate only from March 1, 4 AD onward, when contemporary Julian calendar norms were introduced. Prior to this date, leap year regulations were handled inconsistently, and the Julian calendar did not even exist before 45 BC.
GregorianCalendar is a concrete subclass of a Calendar class that implements the version of the Gregorian Calendar that we are most familiar with, having all of its inherited components implemented either through an interface or an abstract class.
The GregorianCalendar class is a concrete subclass of the Calendar class. All of the inherited members of the GregorianCalendar class are implemented. The Calendar class implements the Gregorian calendar, which is widely used. We import the Java.util.GregorianCalendar class in our programme to utilise the Gregorian calendar.
Difference between GregorianCalendar class and Calendar class
GregorianCalendar class | Calendar class |
GregorianCalendar Class can be instantiated. Thus, the initialization of a GregorianCalendar Class object is as follows: | Calendar Class is abstract and cannot be created. As a result, an object of the Calendar Class is initialised as follows: |
GregorianCalendar gcal = new GregorianCalendar(); | Calendar cal = Calendar.getInstance(); |
Here, the default locale and timezone are used to initialize an object of the GregorianCalendar Class called gcal with the current date and time. | Here, the default locale and timezone for an object called cal of the Calendar Class are set to the current date and time. |
Specified categories
The GregorianCalendar Class is divided into two categories:
AD: With reference to the common period(anno Domini)
BC: With referring to before common period(Before Christ)
Constructors
There are various constructors for GregorianCalendar objects. GregorianCalendar constructors typically initialise an object with the default date and time in the user-specified locale and time zone or with the date and time the user provided inside the default locale and time zone. Here are some of them:
Constructors | Description |
GregorianCalendar() | Provides the object with a default locale and time zone setup that includes the time and date of the moment. |
GregorianCalendar(int year, int month, int dayOfMonth) | establishes the default locale, time zone, and date specified as arguments for the object. |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes) | uses the provided date and time as well as the default locale and time zone to initialise the object. |
GregorianCalendar(int year, int month, int dayOfMonth, int hours, int minutes, int seconds) | use the passed-in date and more accurate time in the default locale and time zone to initialise the object. |
GregorianCalendar(Locale locale) | initialises the object with the specified locale, current date, and current time in the specified time zone. |
GregorianCalendar(TimeZone timeZone) | updates the object's default locale to the supplied locale, the specified time zone, and the current date and time. |
GregorianCalendar(TimeZone timeZone, Locale locale) | initializes the object with the specified time zone and locale's current date and time. |
GregorianProgram.java
// Java program that displays the Calendar class
// GregorianCalendar class and default instantiation
// with a default constructor, both are essentially equivalent.
// revert to using the Gregorian Calendar as the default
// time zone, date, and locale
import java.util.Calendar;
import java.util.GregorianCalendar;
class GregorianProgram{
public static void main(String[] args)
{
// constructing a Calendar Class object
Calendar cal = Calendar.getInstance();
/* Constructing a GregorianCalendar Class object */
GregorianCalendar gcal = new GregorianCalendar();
/* Display the Current Date Using the Calendar Class */
System.out.println("Day of the week: "+ cal.getTime());
/* Utilizing the GregorianCalendar Class to Display the Current Date */
System.out.print("Date of Gregorian Calendar: "+ gcal.getTime());
}
}
Output:
Example of how to use various constructors:
1. Using the default constructor
GregorianCalendarProgram.java
// A Java application that displays a basic Gregorian calendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// declaring a collection to hold month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// establishing a collection to hold AM or PM
String amPm[] = { "AM", "PM" };
/* Making a GregorianCalendar class object using the default constructor*/
GregorianCalendar gcal = new GregorianCalendar();
// displays the time, date, time zone, and location
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"+ "Location: "+ Locale.getDefault().getDisplayName());
}
}
Output:
2. Using the parameters year, month, and dayOfMonth
GregorianCalendarProgram.java
// A Java application that displays a basic Gregorian calendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// establishing a range that contains month abbreviations
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* Gregorian Calendar class object creation requires the year, month, and dayOfMonth parameters */
GregorianCalendar gcal = new GregorianCalendar(2022, 10, 22);
// showing the date, time, time zone, and location
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"+ "Location: "+ Locale.getDefault().getDisplayName());
}
}
Output:
3. By each succeeding year, month, day of the month, hour of the day, and minute:
GregorianCalendarProgram.java
// A Java application that demonstrates a primitive Gregorian calendar.
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// creating a collection to hold abbreviations for the months
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* By providing the year, month, day of the month, the hour of the day, and minute when creating a GregorianCalendar class object */
GregorianCalendar gcal = new GregorianCalendar(2022, 10, 23, 8, 12);
// The time, date, time zone, and location are displayed
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"+ "Location: "+ Locale.getDefault().getDisplayName());
}
}
Output:
4. From each successive year, month, day of the month, hour of the day, minute, and second:
GregorianCalendarProgram.java
// A Java application that demonstrates a basic Gregorian calendar
// operations
import java.util.Locale;
import java.util.TimeZone;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// creating a collection to hold abbreviations for the months
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* GregorianCalendar objects are created by defining the year, month, day of the month, hour of the day, minute, and second. */
GregorianCalendar gcal = new GregorianCalendar(2022, 10, 23, 4, 10, 15);
// The time, date, time zone, and location are displayed
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"+ "Location: "+ Locale.getDefault().getDisplayName());
}
}
Output:
5. By specifying timeZone as a parameter:
GregorianCalendarProgram.java
// A Java application that displays a simple Gregorian calendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// creating a collection to hold abbreviations for the months
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* To establish a user-defined time zone (GMT + 5:30), one must construct an object of the TimeZone class and an instance of the GregorianCalendar class.*/
TimeZone tz = TimeZone.getTimeZone("GMT+5:30");
GregorianCalendar gcal = new GregorianCalendar(tz);
// The time, date, time zone, and location are displayed
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"+ "Location: " + Locale.getDefault().getDisplayCountry());
}
}
Output:
6. By including the locale as a parameter:
GregorianCalendarProgram.java
// A Java application that displays a basic Gregorian calendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// creating a collection to hold abbreviations for the months
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* Creating an instance of the Locale class to generate an object of the GregorianCalendar class to attach a user-specified locale (India)*/
Locale l = new Locale("en", "IN");
GregorianCalendar gcal = new GregorianCalendar(l);
// The time, date, time zone, and location are displayed
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"+ "Location: " + l.getDisplayCountry());
}
}
Output:
7. By including the parameters for timeZone and locale:
GregorianCalendarProgram.java
// A Java program that displays a basic Gregorian calendar
// operations
import java.util.TimeZone;
import java.util.Locale;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class GregorianCalendarProgram {
public static void main(String args[])
{
// creating a collection to hold abbreviations for the months
String month[] = { "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec" };
// generating a collection to store AM or PM
String amPm[] = { "AM", "PM" };
/* To specify a user-defined time zone (GMT + 5:30) and locale, one must first construct an object of the TimeZone class and an object of the Locale class (India)*/
TimeZone tz = TimeZone.getTimeZone("GMT+5:30");
Locale l = new Locale("en", "IN");
GregorianCalendar gcal = new GregorianCalendar(tz, l);
// The time, date, time zone, and location are displayed
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"+ "Location: " + l.getDisplayCountry());
}
}
Output: