Wednesday, July 3, 2024
HomeLanguagesJavaWeekFields dayOfWeek() method in Java with Examples

WeekFields dayOfWeek() method in Java with Examples

The dayOfWeek() method of WeekFields class is used to return a field to access the day of the week based on this WeekFields. For example, if the first day-of-week is Monday, then that will have the value 1, with other days ranging from Tuesday as 2 to Sunday as 7.

Syntax:

public TemporalField dayOfWeek()

Parameters: This method accepts nothing.

Return value: This method returns a field providing access to the day-of-week with localized numbering, not null.

Below programs illustrate the WeekFields.dayOfWeek() method:
Program 1:




// Java program to demonstrate
// WeekFields.dayOfWeek() method
  
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create WeekFields
        WeekFields weekFields
            = WeekFields.of(DayOfWeek.MONDAY, 1);
  
        // apply dayOfWeek()
        TemporalField dayOfWeek
            = weekFields.dayOfWeek();
  
        // create a LocalDate
        LocalDate day = LocalDate.of(2021, 12, 21);
  
        // get day of week for localdate
        int dow = day.get(dayOfWeek);
  
        // print results
        System.out.println("day of week for "
                           + day + " :" + dow);
    }
}


Output:

day of week for 2021-12-21 :2

Program 2:




// Java program to demonstrate
// WeekFields.dayOfWeek() method
  
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create WeekFields
        WeekFields weekFields
            = WeekFields.of(DayOfWeek.SUNDAY, 1);
  
        // apply dayOfWeek()
        TemporalField dayOfWeek
            = weekFields.dayOfWeek();
  
        // create a LocalDate
        LocalDate day
            = LocalDate.of(2018, 05, 31);
  
        // get day of week for localdate
        int dow = day.get(dayOfWeek);
  
        // print results
        System.out.println("day of week for "
                           + day + " :" + dow);
    }
}


Output:

day of week for 2018-05-31 :5

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/WeekFields.html#dayOfWeek()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments