Javatpoint Logo
Javatpoint Logo

Gregorian Calendar Java Current Date

The Gregorian calendar is still the most popular calendar scheme in use today. It replaced the Julian calendar, which had been in use since 45 BC, and was adopted by Pope Gregory XIII in 1582. The Gregorian calendar is a solar calendar, which means that it is founded on the Earth's orbit around the sun.

Java is a well-liked programming language that enables programmers to build applications that manipulate dates and times. In this section, will go over how to get the current date in Java using the Gregorian calendar.

Getting the Current Date

Java's Calendar class can be used to obtain the current date in Gregorian format. With the help of the Calendar class, it is possible to change days and times without having to worry about the Gregorian calendar's specifics.

An illustration of Java code that retrieves the current date using the Gregorian calendar and displays it to the console is provided below:

CalendarDemo.java

Output:

Today's date is 2023-04-05

Using the getInstance() method, which gives a Calendar instance initialised with the current date and time in the default time zone and locale, we first create a Calendar object in this example.

The year, month, and day of the month are then obtained from the Calendar object using the get() function. Because the MONTH field uses a zero basis, we must add 1 to obtain the correct month number.

Finally, we display the date in the format yyyy-MM-dd to the console using the printf() method. The year is extended to four digits with the %04d specifier and the month and day are extended to two numbers with the %02d specifier.

Conclusion

In this tutorial, we covered how to use the Gregorian calendar in Java to find the current date. The Calendar class offers a convenient and adaptable method to manipulate times and dates without having to worry about the specifics of the calendar system. We can quickly acquire the current date and time by using the Calendar class's methods, as well as carry out a number of operations on dates and times, like adding or subtracting days, months, or years.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA