Studytonight is now part of the GUVI universe. Explore GUVI →
🤩 New Cool Developer Tools for you. Explore →
FREE JavaScript Video Series Start Learning →
Signup/Sign In
Tests
MCQs to test your knowledge.
Compilers
Compilers to execute code in browser.
Index
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.

[フレーム]



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.

Learn to Code
Learn and practice coding side-by-side.
NEW
C language Course
115+ coding exercises
Javascript Course
85+ coding exercises

AltStyle によって変換されたページ (->オリジナル) /