Java Date Time - LocalDate getDayOfWeek() example








LocalDate getDayOfWeek() gets the day-of-week field, which is an enum DayOfWeek.

This method returns the enum DayOfWeek for the day-of-week.

Syntax

getDayOfWeek has the following syntax.

public DayOfWeek getDayOfWeek()

Example

The following example shows how to use getDayOfWeek.

import java.time.LocalDate;

public class Main {
  public static void main(String[] args) {
    LocalDate a = LocalDate.of(2014, 6, 30);

    System.out.println(a.getDayOfWeek().name());
  }
}

The code above generates the following result.





Example 2

Get day of week for a given LocalDate

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
//from  w  w w  . j  av a 2  s  . co  m
public class Main {
  public static void main(String... args) {
    LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
    DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
    System.out.println(dayOfWeek);    // FRIDAY


  }
}

The code above generates the following result.