Java Data Type How to - Get year, month and day of month from local date








Question

We would like to know how to get year, month and day of month from local date.

Answer

import java.time.LocalDate;
//from  w w w. ja va2 s .c o  m
public class Main {
  public static void main(String[] args) {
    // Human Readable
    LocalDate date = LocalDate.now();
    System.out.println(String.format("%s-%s-%s", date.getYear(),
        date.getMonthValue(), date.getDayOfMonth()));

  }
}

The code above generates the following result.