All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Get Current Year In Java

Last Updated : Mar 11, 2024

Get Current Year In Java

In this article we will show you the solution of get current year in java, classes LocalDate. In order to provide a more advanced and complete API for manipulating date and time, the java.time package was initially developed in Java 8.

Without using outdated date-time classes or doing manual computations, we can quickly access the current year by utilising the LocalDate class.

In Java programming, dealing with dates and times is frequently necessary.

To execute various activities or show information based on the current year, we may occasionally need to retrieve the current year.

This article will examine various methods for finding the current year in Java, with a focus on the Java 8-introduced java.time package.

We will use java.time to find the current year in Java most particularly the now() method of the LocalDate type.

The year, month, and day can all be accessed using this method, which returns the present date in the form of LocalDate object.

A Java.time. The LocalDate class offers a practical method for working with dates without taking the time or time zones into account.

Here's how to use LocalDate to obtain the current year. A Java.time. In the ISO-8601 calendar, a year is represented by a year class. By using it, we may obtain the current year.

Step By Step Guide On Get Current Year In Java :-

Package year;
import java.time.LocalDate;
public class Year {
    public static void main(String[] args) {
        LocalDate CD = LocalDate.now();
        int CY = CD.getYear();
        System.out.println("Current Year is: " + CY);
    }
}
  1. The package introduction package Year; is the first line in the code. This line indicates that the class is a component of the "Year" package.
  2. import java.time is the next line.LocalDate imports the Java.time package's LocalDate class. The techniques for using dates are covered in this session.
  3. Public class year is where the class definition starts. It defines the "year" public class.
  4. The main method within the class has the declaration public static void main(String[] args). The starting point for the execution of the programme is this approach.
  5. The now() function of the LocalDate class is used to create a new LocalDate object called CD and assign it the current date in the line LocalDate CD = LocalDate.now().
  6. the statement int CY = CD.The LocalDate class's getYear() method is used to retrieve the year from a CD object. It gives the integer variable CY the year value.
  7. At the end, the statement System.out.println("Current Year is: " + CY); prints the current year by adding the string "Current Year is: " to the value of CY. The result is shown on the console with the println method.

Conclusion :-

In this article, we learnt how to use Java's java.time to get the current year.Classes LocalDate.

We may quickly retrieve the current year by using the now() and getYear() methods instead of laborious calculations or out-of-date date-time classes.

With the help of the java.time package, working with times and dates in Java is made simpler and more streamlined, making it simpler to perform a variety of date-related activities effectively.

I hope this article on get current year in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪