Java Localdate Getdayofweek Method

JavaJavaBeginner
Practice Now

Introduction

The getDayOfWeek() method is used to retrieve the day of the week of a given date. It is a part of the java.time.LocalDate class in Java. This method returns an object of the java.time.DayOfWeek enum that represents the day of the week for the given date.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/date("`Date`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/classes_objects -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/class_methods -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/date -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/modifiers -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/oop -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/packages_api -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/identifier -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/arrays -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/data_types -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/operators -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/output -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/strings -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/object_methods -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} java/system_methods -.-> lab-117788{{"`Java Localdate Getdayofweek Method`"}} end

Create a new Java file

Navigate to the project directory and create a new Java file named GetDayOfWeekDemo.java.

cd ~/project
touch GetDayOfWeekDemo.java

Import required packages and create a main method

Now, open the GetDayOfWeekDemo.java file in your text editor and import the following packages:

import java.time.DayOfWeek;
import java.time.LocalDate;

After importing the packages, create a main method inside the GetDayOfWeekDemo class.

public class GetDayOfWeekDemo {
  public static void main(String[] args) {

  }
}

Define a LocalDate object

Inside the main method, create a LocalDate object with a specified date using the of() method.

LocalDate localDate = LocalDate.of(2021, 8, 25);

Get the day of the week using getDayOfWeek()

Now that we have a LocalDate object, we can use the getDayOfWeek() method to retrieve the day of the week for the specified date.

DayOfWeek dayOfWeek = localDate.getDayOfWeek();

Print the day of the week

We can now print the day of the week using the toString() method on the DayOfWeek object.

System.out.println("Day of the week: " + dayOfWeek.toString());

Compile and run the program

Save the changes to the file and compile the GetDayOfWeekDemo.java file using the javac command.

javac GetDayOfWeekDemo.java

After successful compilation, execute the program using the java command.

java GetDayOfWeekDemo

This will output the day of the week for the specified date.

Try with different date

Modify the date in the LocalDate object to get the day of the week for a different date.

LocalDate localDate = LocalDate.of(2021, 8, 30);

Try with current date

To get the day of the week for the current date, we can simply create a LocalDate object with no arguments.

LocalDate localDate = LocalDate.now();

Try with more complex example

To get the day of the week for a date far in the future, let's say in the year 2050, simply change the values passed to the of() method.

LocalDate localDate = LocalDate.of(2050, 12, 25);

Summary

In this lab, we learned how to use the Java getDayOfWeek() method to retrieve the day of the week for a given date. Follow the above step-by-step guide to run the code successfully in the terminal of the Ubuntu system.

Other Java Tutorials you may like