How to get Day Month and Year from LocalDate in Java

This example shows how to get a day, month and year from LocalDate class in Java.

LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.

Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.

Java LocalTime now() Method Example

LocalDate class provides below APIs to get a year, month and day respectively.
  • Month getMonth() - Gets the month-of-year field using the Month enum.
  • int getMonthValue() - Gets the month-of-year field from 1 to 12.
  • int getYear() - Gets the year field.
  • int getDayOfMonth() - Gets the day-of-month field.
  • DayOfWeek getDayOfWeek() - Gets the day-of-week field, which is an enum DayOfWeek.
  • int getDayOfYear() - Gets the day-of-year field.
import java.time.LocalDate;
/**
 * Program to demonstrate LocalDate Class APIs.
 * @author javaguides.net
 *
 */
public class LocalDateExamples {
 
    public static void main(String[] args) {
        getYearMonthDay();
    }
 
    private static void getYearMonthDay() {
        LocalDate localDate = LocalDate.now();
        System.out.println("Year : " + localDate.getYear());
        System.out.println("Month : " + localDate.getMonth().getValue());
        System.out.println("Day of Month : " + localDate.getDayOfMonth());
        System.out.println("Day of Week : " + localDate.getDayOfWeek());
        System.out.println("Day of Year : " + localDate.getDayOfYear());
    }
}
Output:
Year : 2018
Month : 8
Day of Month : 10
Day of Week : FRIDAY
Day of Year : 222

Reference



Comments