Java LocalDate getYear Method

JavaJavaBeginner
Practice Now

Introduction

The getYear() method in Java LocalDate class returns the year of a date. In this lab, we will learn how to use the getYear() method with examples.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/classes_objects -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/class_methods -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/date -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/modifiers -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/oop -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/packages_api -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/identifier -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/arrays -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/data_types -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/operators -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/output -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/strings -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/variables -.-> lab-117800{{"`Java LocalDate getYear Method`"}} java/system_methods -.-> lab-117800{{"`Java LocalDate getYear Method`"}} end

Create a Java Class

First, open a text editor and create a new Java file called LocalDateGetYear.java in the ~/project directory.

cd ~/project
touch LocalDateGetYear.java

Import necessary class

The following code block is used to import the necessary class.

import java.time.LocalDate;

Create a LocalDate object

Create a LocalDate object specifying a particular date of which you want to get the year from. In this example, we will use the date "2021-10-10".

LocalDate localDate = LocalDate.of(2021, 10, 10);

Get the year from LocalDate object

Call the getYear() method on the LocalDate object created earlier to get the year of the date.

int year = localDate.getYear();

Print the year

Print the year to the console using the System.out.println() method.

System.out.println("Year of date: " + year);

Compile and Run the code

Save the file and close the text editor. Open the terminal and navigate to the ~/project directory. Type the following command to compile the code.

javac LocalDateGetYear.java

Then, type the following command to run the code.

java LocalDateGetYear

Complete the Code

The complete code for the LocalDateGetYear.java file should look like this:

import java.time.LocalDate;

public class LocalDateGetYear {
    public static void main(String[] args) {
        LocalDate localDate = LocalDate.of(2021, 10, 10);
        int year = localDate.getYear();
        System.out.println("Year of date: " + year);
    }
}

Test with Different Dates

Modify the date used in the LocalDate.of() method to test the code with different dates.

LocalDate localDate = LocalDate.of(2000, 12, 31);
int year = localDate.getYear();
System.out.println("Year of date: " + year);

Use LocalDate.now() method to get current date

Instead of specifying a specific date, get the current date using the now() method of the LocalDate class.

LocalDate localDate = LocalDate.now();
int year = localDate.getYear();
System.out.println("Year of date: " + year);

Compile and Run the code

Save the file and close the text editor. Open the terminal and navigate to the ~/project directory. Type the following command to compile the code.

javac LocalDateGetYear.java

Then, type the following command to run the code.

java LocalDateGetYear

Summary

In this lab, we have learned how to use the getYear() method of the LocalDate class to get the year of a date in Java. We have also learned how to create a LocalDate object, call its getYear() method, and print the year to the console. We have also seen how to use the now() method to get the current date and get its year.

Other Java Tutorials you may like