Get the Current Year in Java

Siddharth Swami Jan 30, 2023 Jun 13, 2021
  1. Use the java.Util.Date Class to Get the Current Year in Java
  2. Use the LocalDate Class to Get the Current Year in Java
  3. Use the java.util.Calendar Class to Get the Current Year in Java
  4. Use the Joda Time Package to Get the Current Year in Java
  5. Use the java.time.YearMonth Class to Get the Integer Value of the Current Year in Java
Get the Current Year in Java

In this tutorial, different methods are discussed on how to get the current year in Java.

Use the java.Util.Date Class to Get the Current Year in Java

The getYear() method present in the java.util.Date class can be used to get the current year in the older versions of JDK, which is now replaced with the Calendar.get(Calendar.YEAR) method.

This method is used to return the value by subtracting 1900 from the current date object.

See the following code.

import java.util.Date;

public class Java_Date_GetYear{
    public static void main(String[] args) {
        Date dt=new Date();
        int year=dt.getYear();
        System.out.println("Year for date object is : "+year);
        int current_Year=year+1900;
        System.out.println("Current year is : "+current_Year);
    }
}

Output:

Year for date object is : 121
Current year is : 2021

As we can see in the above output, we get the date object as 121, but we need to get the current year. We have to add 1900 again to the date object to get the final result, i.e., 2021.

Use the LocalDate Class to Get the Current Year in Java

The concept of this class is to use the methods provided under this class, like getyear(), getMonth(), etc., to get the current year or the month as needed by the user.

For example,

import java.time.LocalDate;

class Current_Year {
        public static void main(String args[]) {
            //Getting the current date value of the system
            LocalDate current_date = LocalDate.now();
            System.out.println("Current date: "+current_date);

            //getting the current year from the current_date
            int current_Year = current_date.getYear();
            System.out.println("Current year: "+current_Year);
        }
    }

Output:

Current date: 2021-06-06
Current year: 2021

In the above code, we take the current date, time, and year of the system on which the code is being executed, and then we return the current year in the output window. For this, the now() method of the LocalDate() class is used.

Use the java.util.Calendar Class to Get the Current Year in Java

Another approach is using the Calendar class in Java. This is an abstract Java class that helps convert the date and the calendar fields like a month, year, hour, etc.

The getInstance() method is a static method to initiate the sub-class since the Calendar class is an abstract class. We cannot use the constructor method to create the instance. The get(Calendar.Year) method can provide the current year in the system.

For example,

import java.util.Calendar;
public class Get_Current_Year {
        public static void main(String args[])
        {
            Calendar cal = Calendar.getInstance();
            System.out.println("The Current Year is:" + cal.get(Calendar.YEAR));
        }
    }

Output:

The Current Year is:2021

We can print the current year of a specific date using the Calendar class. For this, we have to use another sub-class of the Calendar known as GregorianCalendar().

For example,

import java.util.*;
  
class Get_Current_Year2 {
  
    public static void main(String args[])
    {
        // Creating a calendar object
        Calendar c
            = new GregorianCalendar(
                2020, 02, 11);
  
        // Getting the value of the year from calendar object
        int year = c.get(Calendar.YEAR);
  
        // Printing the year
        System.out.println("Year: " + year);
    }
}

Output:

Year: 2020

Use the Joda Time Package to Get the Current Year in Java

If there is heavy usage of date and calendar objects in your application, then you should use the Joda Time package. The java.util.Date class is mutable, and the java.util.calendar class has some problems related to performance when it updates its fields.

Joda Time has more effective methods than the class in packages in Java.

See the following code.

import org.joda.time.DateTime;
import org.joda.time.LocalDateTime;
public class Joda_Time {
    public static void main(String[] args)
    {
       DateTime now = new DateTime();
       System.out.println("Current Year: " + now.year().getAsText());
    }
}

Output:

Current Year : 2021

As we can see in the above code, we created an object of the DateTime() used to get the system’s current date. To use Joda-Time, we have to download it since it is not pre-installed in any IDE.

Use the java.time.YearMonth Class to Get the Integer Value of the Current Year in Java

We can also use the java.time.YearMonth class, which provides us with the getYear() method to print the current year of the system.

The code below demonstrates the use of this method.

import java.time.YearMonth;
public class Current_Year {
    public static void main(String[] args) {
        int year = YearMonth.now().getYear();
        
        System.out.println("Current year: "+ year);
    }
}

Output:

Current year: 2021

Related Article - Java DateTime