Java - getXXX( ) Methods

Introduction

getXXX() method returns the specified element of the object.

For example, the getYear() method in the LocalDate object returns the year part of the date.

The following snippet of code shows how to get year, month, and day from a LocalDate object:

Demo

import java.time.LocalDate;
import java.time.Month;
public class Main {
  public static void main(String[] args) {
    LocalDate ld = LocalDate.of(2012, 5, 2);
    int year = ld.getYear();       // 2012
    Month month = ld.getMonth();   // Month.MAY
    int day = ld.getDayOfMonth();  // 2

    System.out.println(year);// w w w .j  a  v  a 2 s .  c om
    System.out.println(month);
    System.out.println(day);
  }
}

Result