Java LocalDate Class | getDayOfWeek() Method with Example

LocalDate Class getDayOfWeek() method: Here, we are going to learn about the getDayOfWeek() method of LocalDate Class with its syntax and example.
Submitted by Preeti Jain, on May 29, 2020

LocalDate Class getDayOfWeek() method

  • getDayOfWeek() method is available in java.time package.
  • getDayOfWeek() method is used to get the value for the field day-of-week that is an enum DayOfWeek of this LocalDate.
  • getDayOfWeek() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getDayOfWeek() method does not throw an exception at the time of getting the day of the week.

Syntax:

    public DayOfWeek getDayOfWeek();

Parameter(s):

  • None

Return value:

The return type of this method is DayOfWeek, it returns the field value day-of-week.

Example:

// Java program to demonstrate the example 
// of getDayOfWeek() method 
// of LocalDate

import java.time.*;

public class GetDayOfWeekOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-05");
        LocalDate l_da2 = LocalDate.now();

        // Display l_da1,l_da2
        System.out.println("LocalDate l_da1 and l_da2: ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

        // Here, this method gets the value for
        // the field day-of-week in this
        // date object l_da1 that is based
        // on an enum DayOfWeek
        DayOfWeek dow = l_da1.getDayOfWeek();

        // Display dow
        System.out.println("l_da1.getDayOfWeek(): " + dow);

        // Here, this method gets the value for
        // the field day-of-week in this
        // date object l_da2 that is based
        // on an enum DayOfWeek
        dow = l_da2.getDayOfWeek();

        // Display dow
        System.out.println("l_da2.getDayOfWeek(): " + dow);
    }
}

Output

LocalDate l_da1 and l_da2: 
l_da1: 2007-04-05
l_da2: 2020-05-29

l_da1.getDayOfWeek(): THURSDAY
l_da2.getDayOfWeek(): FRIDAY


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.