In Java, the built-in classes like Clock, Instant, LocalDate, OffsetDateTime, YearMonth, etc. offer various functions to get the current year. These classes access and manipulate dates and times using local or specific time zones. Also, these Java classes help us get/extract only a specific date field, such as “year”.

This article provides multiple methods for getting the current year in Java.

Method 1: Get the Current Year Using Year class

The java.time.Year” represents a year that is an immutable entity that follows the ISO-8601 format e.g., 2012. The Year class inherits the functionality of the Object class. Furthermore, it also implements the Java interface such as “Comparable” interface. In the code given below, the Year class object is created to check the current year in Java.

The import statement includes the necessary dependencies and classes from the java.time package such as Year class. Inside the main() method, the “nowyear” is an instance of the Year class that stores the current year’s value.

package getCurrentYear;
import java.time.*;  //importing the dependency
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
Year nowyear=Year.now(); //current year
System.out.println("Current Year :  " + nowyear); //output
}
}

Output 

The output of the code is as follows:

Alternate Method 

The getValue() function of the Year class returns the year’s value in the integer format. The code given below determines the current year using the Year.getValue() function:

package getCurrentYear;
import java.time.*;  //import the dependency
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
System.out.println("Current Year : " +  Year.now().getValue()); //output
}
}

Output 

The output of the code is given below:

Method 2: Get the Current Year Using the Clock Class 

The Java Clock class is a part of the Date-time API that was introduced in Java 8. Almost all the date-time classes are imported from the java.time package and contains the now() function. Similarly, the Clock class in Java also provides access to the date and time based on a specific timezone.

The import statement imports the important classes and functions such as Year and Clock etc. The systemUTC() method returns the current instance of time and the Year.now() extracts the current year from it: 

package getCurrentYear;
import java.time.*;  //importing the dependency
public class getCurrentYear {
public static void main(String[] args)  //Main method{
System.out.println("The Year is : " + Year.now(Clock.systemUTC()));
}
}

Output 

The output of the code is as follows:

Method 3: Get the Current Year Using Calendar Class 

The Calendar class in Java provides different methods for working with date and time. It is an abstract class that is provided by the java.util package. 

In the following code, the import statement includes the Calender class from java.util package. The getInstance() is a static method that returns an instance of current time. The “get(Calendar.Year)” returns the calendar value of the specified field. The value of the year is then printed using the print statement:

package getCurrentYear;
import java.util.Calendar;
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
int year = Calendar.getInstance().get(Calendar.YEAR);
System.out.println("Present Year is : " + year);
}
}

Output 

The output is shown below:

Method 4: Get the Current Year Using ZoneId Class

In Java, the ZoneId identifies and provides a set of rules for the conversion between the Instant and LocalDateTime. To get the current year in Java, the ZoneId class is used in association with the Year class. 

For this, import the necessary dependencies and classes as shown in the code. Inside the main() method, the Year class invokes the now() method that extracts the current year based on the time zone. The systemDefault() method of the ZoneId class returns the default timezone of the system from which the year is determined:

package getCurrentYear;
import java.time.Year; //import Year
import java.time.ZoneId; //import ZoneID
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
    System.out.println("Year is : " + Year.now(ZoneId.systemDefault()));//output
}
}

Output

The output of the code is given as follows:

Method 5: Get the Current Year Using the LocalDate Class

LocalDate class in Java provides a different method to represent the current date in the ISO format i.e., “yyyy-mm-dd”. It is an immutable class that implements the ChoronoLocalDate interface. Developers can import this class from the java.time package.

In the following code, the import statements include the LocalDate class from java.time package. Within the main() method, the “year” variable of the LocalDate data type stores the current date and time returned by the now() function of the LocalDate class. 

package getCurrentYear;
import java.time.LocalDate; //imports the dependency
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
  LocalDate year = LocalDate.now();
  System.out.println("Present Year is : " + year.getYear()); //output
}
}

Output 

Below is the output of the above-given code:

Method 6: Get the Current Year Using the Gregorian Class

The Gregorian class in Java is a part of java.util package that provides methods for dealing with date and time. The date and time are accessed and manipulated using the Gregorian calendar implementation. 

In this code, the GregorianCalender class is imported from the java.util package using the import statement. In the main() method, the Gregorian class object “Obj” is created. This object invokes the get() method that returns the calendar value of the specified field:

package getCurrentYear;
import java.util.GregorianCalendar;
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
  //an instance is created of the given class
  GregorianCalendar Obj = new GregorianCalendar();
 
  // Prints the Current Year
  System.out.println("Present Year is : " + Obj.get(GregorianCalendar.YEAR));
}
}

Output

The output of the code is as follows:

Method 7: Get the Current Year Using the YearMonth Class

The YearMonth class is a part of the Date-time API that returns the current date in the format of “year-month” e.g., 2024-06. It is also provided by the java.time package. 

Like the other date-time classes, the YearMonth class also uses the now() method for specifying the current date and time. The getYear() method only extracts the year present in the format. The code is as follows:

package getCurrentYear;
import java.time.YearMonth;
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
System.out.println("Current Year is : "+ YearMonth.now().getYear());
}
}

Output

The output of the code is as follows:

Method 8: Get the Current Year Using the ZonedDateTime Class 

The ZonedDateTime class is an immutable date-time that stores all the values of the date-time including the nanoseconds values. To get the current year using the ZonedDateTime class, below is the code given

In this code, the import statements are used to include the ZoneId and ZonedDateTime classes. The ZonedDateTime.now() returns the current date and time. The ZoneId specifies the zone, and the getYear() method extracts only the year which is then stored in the “year” variable. The value is then printed:

package getCurrentYear;
import java.time.ZoneId;  //imports ZoneID
import java.time.ZonedDateTime;  //imports ZoneDageTime
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
int year = ZonedDateTime.now(  ZoneId.of( "Asia/Karachi" )  ).getYear() ;
System.out.println("Current Year is : "+ year);
}
}

Output

The output of the code is given below: 

Method 9: Get the Current Year Using the SimpleDateFormat Class 

The SimpleDateFormat class is used to format the provided date and time. The usual format followed is “yyyy/mm/dd”. To determine the current year in Java, the SimpleDateFormat class can be used in the following manner.

The import statements are used to include the SimpleDateFormat and Date class as shown in the snippet below:

import java.text.SimpleDateFormat;
import java.util.Date;

The Date class is used to create a date object. The value of the date is printed using the print statement:

Date date=new Date();
System.out.println("Today date is : " + date);

The SimpleDateFormat class creates a “dateFormat” object with the following format specified in the constructor. The “dateFormat” object invokes the format() function that modifies the date object accordingly:

SimpleDateFormat dateFormat=new SimpleDateFormat("yyy");
System.out.println("The current year is : " + dateFormat.format(date));

Output 

The output of the code is given as follows:

Method 10: Get the Current Year Using LocalDateTime Class

The LocalDateTime is another class of java.time package for dealing with time and date. In Java, the current year can also be determined by using this class. For this, the code is given below.

In the following code, the import statement includes the LocalDateTime class from the “java.time package. Within the main() method, the LocalDateTime object “year” stores the current time in the format of “yyyy-mm-dd-hh-mm-ss.zzz”. The getYear() method invoked by the year object extracts the current year and the output is then printed:

package getCurrentYear;
import java.time.LocalDateTime;
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
    LocalDateTime year= LocalDateTime.now();
    System.out.println("The year is : " + year.getYear());
}
}

Output 

The output is as follows:

Method 11: Get the Current Year Using the OffsetDateTime Class

The OffsetDateTime class represents the date and time with an offset and a nanosecond precision. This class is efficient enough to represent the time difference between the local zone and UTC. You can determine the current year in Java through this class. 

In the given code, the java.time package imports the OffsetDateTime class. The now() method returns the current instance of time. Inside the print statement, the getYear() method extracts the year value which is displayed as output:

package getCurrentYear;
import java.time.OffsetDateTime; //import dependency
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
  OffsetDateTime year = OffsetDateTime.now();
  System.out.println("Current year is : " + year.getYear()); //prints output

}
}

Output

The output is as follows:

Method 12: Get the Current Year Using the Instant and ZoneId Classes 

In Java, the Instant class is imported from the java.time package that represents and records a specific point on a given timeline. Unlike the Date class, the precision in the Instant class is measured in the nanosecond. The Instant Class in conjunction with the ZoneId is used to determine the current year in Java. 

The java.time imports necessary classes such as Instant and ZonedDateTime classes. Inside the main() method, the Instant class creates an object “instant” that stores the specific instance of time. The instant object invokes the atZone() method. Inside this method, the ZoneId is used to determine the default zone using the systemDefault() method. The object zonedObj determines the current year by using the getYear() method:

package getCurrentYear;
import java.time.*;
public class getCurrentYear {
public static void main(String[] args)  //Main method
{
Instant instant = Instant.now();
ZonedDateTime zonedObj= instant.atZone(ZoneId.systemDefault());
System.out.println("Current year is : " + zonedObj.getYear());

}
}

Output 

Below is the output of the given code:

Method 13: Get the Current Year Using Date Class

The java.util.Date class provides multiple built-in methods for accessing and working with date and time. The date and time from the Date class record the present instance of the time with millisecond precision. The Date.getYear() method is a deprecated function that is used to get the current year in Java. 

The import statement includes the Date class from java.util package:

import java.util.Date;

The Date class creates an object “date” using the new keyword. The getYear() method is called using the date object and the result is printed:

Date date=new Date();
System.out.println("Value Returned is : " + date.getYear());

The date.getYear() returns “124” as the output. The integer variable “presentYear” stores the result by adding 1900 to the date.getYear() method. The output is then printed that displays the current year:

int presentYear= 1900+date.getYear();
System.out.println("Present year is : " + presentYear);

Complete Code & Output

The output of the code is given as follows:

That is all from this article. 

Conclusion

To get the current year in Java, different instances of multiple classes such as Instant, Gregorian, Clock, YearMonth, etc. are used as shown in this article. Often, these classes are a part of java.time package and Date-time API. The now() method is also very common to these classes which returns the current date and time of the system clock. By using these classes and instances, you can easily get the current date, time, and year. This article is an extensive guide for getting the current year in Java.