LAST UPDATED: NOVEMBER 24, 2020
Java LocalDate getYear() Method With Examples
Java getYear()
method is used to get the year of a date. This method returns an integer value that represents the year. For example, we have a date 2015/02/01, the result would be 2015.
This method belongs to the LocalDate
class. It does not take any parameter and returns an integer value. The syntax of the method is given below.
Syntax
public int getYear()
Parameters
This method does not take any parameter.
Return
It returns an integer value.
Time for an Example:
Let's take an example to get year of a date. In this example, we are are using the getYear()
method that returns an integer value.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.of(2015, 10, 21);
System.out.println(localDate);
int year = localDate.getYear();
System.out.println("Year of date : "+year);
}
}
2015-10-21
Year of date : 2015
Time for another Example:
Let's take another example to get the current year of current date. Here we have the current date using now()
method that returns the current date.
import java.time.LocalDate;
public class DateDemo {
public static void main(String[] args){
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
int year = localDate.getYear();
System.out.println("Year of date : "+year);
}
}
2020-06-04
Year of date : 2020
Live Example:
Try with a live example, execute the code instantly with our powerful Online Java Compiler.