×

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

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Java Enhanced For Loop (For-each Loop)

JAVA ENHANCED FOR LOOP The enhanced for loop is also called the for-each loop and has some advantages over the regular for loop. A for-each loop is designed to cycle through...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.