Java LocalDate getDayOfWeek() Method with Examples

In this article we are going to see the use of Java LocalDate class getDayOfWeek() method with suitable examples.

Java LocalDate getDayOfWeek() Method with Examples

Explanation:

This java.time.LocalDate.getDayOfWeek() method is used to get the day of the week which date is given by the user. It returns the day of the week which is from Sunday to Saturday.

Syntax:

public DayOfWeek getDayOfWeek()

Let’s see some example programs to understand it more clearly.

Approach:

  • Create an object of localDate class.
  • Then use the getDayOfWeek() method for this particular date to print the day of that week.
  • Print the final result.

Example-1

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-11");
        //Use the getDayOfWeek() method and print the result
        System.out.println("Day-of-the-week: "+date.getDayOfWeek()); 
    }
}


Output:

Day-of-the-week: WEDNESDAY

Example-2

import java.time.LocalDate;
public class Main
{
    public static void main(String[] args)
    {
        //Create an object of LocalDate class and assign a date to it
        //here it parses the local date
        LocalDate date = LocalDate.parse("2022-05-29");
        //Use the getDayOfWeek() method and print the result
        System.out.println("Day-of-the-week: "+date.getDayOfWeek()); 
    }
}
Output:

Day-of-the-week: SUNDAY

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.