Friday, September 5, 2025
HomeLanguagesJavaWeekFields weekOfYear() method in Java with Examples

WeekFields weekOfYear() method in Java with Examples

The weekOfYear() method of WeekFields class is used to return a field to access the week of year based on this WeekFields.
Example:

  • if the 1st day of the year is a Monday, week one starts on the 1st and there is no week zero
  • if the 2nd day of the year is a Monday, week one starts on the 2nd and the 1st is in week zero
  • if the 4th day of the year is a Monday, week one starts on the 4th and the 1st to 3rd is in week zero
  • if the 5th day of the year 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 weekOfYear()

Parameters: This method accepts nothing.

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

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




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


Output:

Week of year for 2021-12-21 :52

Program 2:




// Java program to demonstrate
// WeekFields.weekOfYear() 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 weekOfYear()
        TemporalField weekOfYear
            = weekFields.weekOfYear();
  
        // create a LocalDate
        LocalDate day = LocalDate.of(2014, 10, 12);
  
        // get week of year for localdate
        int wow = day.get(weekOfYear);
  
        // print results
        System.out.println("Week of year for "
                           + day + " :" + wow);
    }
}


Output:

Week of year for 2014-10-12 :42

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

RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS