How to Get the Current Year in Java

Siddharth Swami Feb 02, 2024
  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
  6. Conclusion
How to Get the Current Year in Java

In the dynamic landscape of Java programming, efficient date and time management are paramount. This article explores various methods to obtain the current year in Java, showcasing the versatility of different classes and libraries.

While examining familiar classes like java.util.Date and java.util.Calendar, we also delve into modern solutions provided by the java.time package, highlighting the advantages of using classes such as LocalDate and YearMonth. Additionally, we explore the enduring relevance of the Joda-Time library, offering a robust alternative, especially in environments where Java 8 features might not be readily available.

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.

While the Date class is now considered deprecated in Java 8 and later versions in favor of the more robust java.time package, understanding its usage remains valuable, especially in scenarios where legacy code needs to be maintained.

See the following code:

import java.util.Date;

public class CurrentYearExample {
  public static void main(String[] args) {
    // Get the current date
    Date currentDate = new Date();

    // Extract the year from the current date
    int currentYear = currentDate.getYear() + 1900;

    // Display the result
    System.out.println("Current Year: " + currentYear);
  }
}

In the code example, we begin by importing the java.util.Date class. We then create a class named CurrentYearExample with a main method, which serves as the entry point of our program.

Inside the main method, we instantiate a Date object named currentDate. This object represents the current date and time when the code is executed.

Next, we extract the year component from the Date object using the getYear() method.

It’s crucial to note that the getYear() method returns the number of years since 1900. To obtain the actual current year, we add 1900 to the result.

Finally, we display the current year using the System.out.println statement.

Output:

Current Year: 2023

Use the LocalDate Class to Get the Current Year in Java

The java.time package has become the go-to solution for handling date and time operations. Among the various classes offered by this package, LocalDate stands out as a simple yet powerful choice for dealing specifically with dates.

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.

See the following code:

import java.time.LocalDate;

public class CurrentYearExample {
  public static void main(String[] args) {
    // Get the current date
    LocalDate currentDate = LocalDate.now();

    // Extract the current year
    int currentYear = currentDate.getYear();

    // Display the result
    System.out.println("Current Year: " + currentYear);
  }
}

In the given code snippet, we initiate our journey by importing the java.time.LocalDate class. This class, introduced in Java 8, is part of the comprehensive java.time package that aims to simplify date and time handling.

Within our CurrentYearExample class, we define a main method—our program’s entry point.

The first action involves obtaining the current date, achieved through the LocalDate.now() method. This method returns the current date based on the system clock.

Moving forward, we extract the year component from the obtained LocalDate instance using the getYear() method. Unlike the java.util.Date class, LocalDate directly provides a getYear() method that returns the actual year without requiring adjustments.

To bring our exploration to a close, we display the result using the System.out.println statement, providing a clear representation of the current year.

Output:

Current Year: 2023

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 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.

See the following code:

import java.util.Calendar;

public class CurrentYearExample {
  public static void main(String[] args) {
    // Obtain an instance of the Calendar class
    Calendar calendar = Calendar.getInstance();

    // Extract the current year
    int currentYear = calendar.get(Calendar.YEAR);

    // Display the result
    System.out.println("Current Year: " + currentYear);
  }
}

In the provided code, we embark on our journey to get the current year by first importing the java.util.Calendar class. This class has been a stalwart in Java for handling date and time operations.

Inside our CurrentYearExample class, we begin by obtaining an instance of the Calendar class through the static getInstance() method. This method returns a Calendar object initialized with the current date and time values.

Moving forward, we extract the current year using the get method of the Calendar class, specifying Calendar.YEAR as the field to retrieve. This method returns an integer representing the current year.

To conclude our exploration, we use the System.out.println statement to display the result, providing a clear output of the obtained current year.

Output:

Current Year: 2023

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().

See the following code:

import java.util.GregorianCalendar;

public class CurrentYearExample {
  public static void main(String[] args) {
    // Obtain an instance of the GregorianCalendar class
    GregorianCalendar gregorianCalendar = new GregorianCalendar(2020, 02, 11);

    // Extract the current year
    int currentYear = gregorianCalendar.get(GregorianCalendar.YEAR);

    // Display the result
    System.out.println("Current Year: " + currentYear);
  }
}

Our journey begins with importing the java.util.GregorianCalendar class, a reliable tool for handling date and time operations.

Within the CurrentYearExample class, we initialize an instance of GregorianCalendar using its default constructor. This instance captures the current date and time based on the system clock.

Moving forward, we extract the current year using the get method of the GregorianCalendar class. Here, we specify GregorianCalendar.YEAR as the field to retrieve, ensuring we obtain the year component accurately.

To conclude our exploration, we use the trusty System.out.println statement to display the result. This provides a clear output of the obtained current year, showcasing the effectiveness of the GregorianCalendar class.

Output:

Current 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 remains a robust choice, especially for those working with older codebases or in environments where Java 8 features may not be readily available.

See the following code:

import org.joda.time.DateTime;

public class CurrentYearExample {
  public static void main(String[] args) {
    // Obtain an instance of the DateTime class
    DateTime dateTime = new DateTime();

    // Extract the current year
    int currentYear = dateTime.getYear();

    // Display the result
    System.out.println("Current Year: " + currentYear);
  }
}

In this code snippet, we embark on the journey of obtaining the current year using the Joda-Time library. The first step involves importing the org.joda.time.DateTime class, a fundamental class in Joda-Time for handling date and time.

Within our CurrentYearExample class, we initialize an instance of the DateTime class using its default constructor. This instance encapsulates the current date and time when the code is executed.

Continuing with our quest, we extract the current year using the getYear() method provided by the DateTime class. This method returns an integer representing the year component of the current date and time.

To bring our exploration to a close, we employ the familiar System.out.println statement to display the result, providing a clear output of the obtained current year.

Output:

Current Year: 2023

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. Among these classes, YearMonth stands out as a specialized option for dealing specifically with the year and month components of a date.

See the following code:

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);
  }
}

Now, let’s dive into the intricacies of this code. The first step involves importing the java.time.YearMonth class, a key player in our quest to obtain the current year.

Within our CurrentYearExample class, we initiate the process by acquiring the current YearMonth instance using the YearMonth.now() method. This method dynamically fetches the current year and month based on the system clock.

Moving forward, we extract the current year from the YearMonth instance using the getYear() method. This method specifically targets the year component, providing a concise and direct approach to our objective.

To bring our exploration to fruition, we utilize the trusty System.out.println statement to display the result. The output provides a clear representation of the obtained current year.

Output:

Current Year: 2023

Conclusion

In conclusion, this exploration of obtaining the current year in Java underscores the evolution of date and time handling within the language. While the conventional java.util.Date and java.util.Calendar classes provide legacy solutions, the article emphasizes the importance of transitioning to the more robust java.time package.

Furthermore, it recognizes the enduring value of the Joda-Time library, particularly for developers working in environments where Java 8 features may not be immediately accessible. By understanding these diverse approaches, developers can confidently navigate the complexities of date and time management in Java, ensuring their code aligns with both traditional and modern practices.

Related Article - Java DateTime