How to get the current year in Java
In this tutorial, we are going to learn about how to get the current year as an integer in Java.
Getting the current year
To get the current year, we can use the Year.now()
method by calling a getValue()
on it.
The
getValue()
method returns the current year in four-digit(2021) format according to the user local time.
Here is an example:
import java.time.Year;
public class Main
{
public static void main(String[] args) {
int year = Year.now().getValue();
System.out.println(year);
}
}
Output:
2021
Note: This above method is available in Java 8
java.time.Year
package.