Open In App

WeekFields weekOfWeekBasedYear() method in Java with Examples

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

This field can be used with any calendar system.



Syntax:

public TemporalField weekOfWeekBasedYear()

Parameters: This method accepts nothing.



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

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




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

Output:
Week of week for 2021-12-21 :52

Program 2:




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

Output:
Week of week for 2014-10-12 :42

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


Article Tags :