×

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 classCalendar 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:

ConstructorsDescription
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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

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:

Gregorian Calendar Java Current Date

Related Topics

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

How to Print array in Java?

A Java array is a data structure that allows us to hold components of the same data type. An array's items are kept in a single memory region. As a...

6 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Properties Class in Java

Properties class is associated with Java since JDK 1.0, i.e. it is a legacy class. It is the subclass of Hashtable. It is used to maintain the lists of values in which...

5 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.