All TalkersCode Topics

Follow TalkersCode On Social Media

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

How To Get Current Year In Java

Last Updated : Mar 11, 2024

How To Get Current Year In Java

In this article we will show you the solution of how to get current year in java, in Java, there are numerous ways to find the current year. Using java.time is one option.Java 8 added the Year class from java.time package.

Using the java.util is an additional option.from the java.util package: the calendar class.

A year value is initially taken out of an object that reflects the present time and date in all methods.

The correct methods can be used to return or print the year value to the console once it has been retrieved. We'll talk about how to get the current year in Java now.

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

Method 1 - Using a method named getCurrentYear()

import java.time.Year;
public class GetCurrentYear {
    public static int getCurrentYear() {
        Year currentYear = Year.now();
        return currentYear.getValue();
    }
    public static void main(String[] args) {
        int year = getCurrentYear();
        System.out.println("Current Year: " + year);
    }
}
  1. As you can see, we used the getCurrentYear() function to construct some straightforward Java code to retrieve the current year.
  2. The Year class, which offers capability to represent a year, is imported at the beginning of the code. The java.time package contains it.
  3. Inside the GetCurrentYear class, a getCurrentYear() method is defined that returns the current year as an integer.
  4. Then the getCurrentYear() method creates a Year object using the Year.now() method, which gets the current year value based on the system clock.
  5. Then it returns the current year value as an integer using the getValue() method.
  6. In the main() method, the getCurrentYear() method is called to get the current year value, which is then stored in an int variable named year.
  7. Finally, the System.out.println() method is used to print the current year value to the console with a message.

Method 2 - Using a method named getYearFromCalendar()

import java.util.Calendar;
public class GetCurrentYear {
    public static int getYearFromCalendar() {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        return currentYear;
    }
    public static void main(String[] args) {
        int year = getYearFromCalendar();
        System.out.println("Current Year: " + year);
    }
}
  1. You can see that we use the getYearFromCalender() function to create some straightforward Java code in order to retrieve the current year.
  2. The Calendar class, which offers the ability to interact with dates and times, is imported at the beginning of the code.
  3. An integer-based method called getYearFromCalendar() is defined within the GetCurrentYear class and returns the current year.
  4. Then, using the Calendar, the getYearFromCalendar() function creates a Calendar object.
  5. The present date and time are represented via the getInstance() method.
  6. Next, it retrieves the current year's value as an integer using the Calendar object's get() method and the Calendar.YEAR constant.
  7. The current year value is obtained by calling the getYearFromCalendar() method in the main() method, and it is then saved in the year int variable.
  8. Finally, a message and the value of the current year are printed to the console using the System.out.println() method.

Conclusion :-

As a result, we have successfully learnt how to use Java to find the current year.

We also discovered that one choice is to use java.time.A year class from the java.time package was added in Java 8. Another choice is to use the java.util.

The calendar class is part of the java.util package. A year value is initially taken out of an object that reflects the present time and date in all methods.

I hope this article on how to 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 🡪