WeekFields weekOfMonth() method in Java with Examples

The weekOfMonth() method of WeekFields class is used to return the field to access the week of month based on this WeekFields.
For example:

  • if the 1st day of the month is a Monday, week one starts on the 1st and there is no week zero
  • if the 2nd day of the month is a Monday, week one starts on the 2nd and the 1st is in week zero
  • if the 4th day of the month is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero
  • if the 5th day of the month is a Monday, week two starts on the 5th and the 1st to 4th is in week one

This field can be used with any calendar system.

Syntax:



public TemporalField weekOfMonth()

Parameters: This method accepts nothing.

Return value: This method returns a field providing access to the week-based-year, not null.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// WeekFields.weekOfMonth() 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 weekOfMonth()
        TemporalField weekOfMonth
            = weekFields.weekOfMonth();
  
        // create a LocalDate
        LocalDate day
            = LocalDate.of(2021, 12, 21);
  
        // get week of month for localdate
        int wom = day.get(weekOfMonth);
  
        // print results
        System.out.println("week of month for "
                           + day + " :" + wom);
    }
}

chevron_right


Output:

week of month for 2021-12-21 :4

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// WeekFields.weekOfMonth() 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 weekOfMonth()
        TemporalField weekOfMonth
            = weekFields.weekOfMonth();
  
        // create a LocalDate
        LocalDate day = LocalDate.of(2020, 01, 10);
  
        // get week of month for localDate
        int wom = day.get(weekOfMonth);
  
        // print results
        System.out.println("week of month for "
                           + day + " :" + wom);
    }
}

chevron_right


Output:

week of month for 2020-01-10 :2

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

java-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.