The WeekFields class represents the definition of the week, to provide TemporalField instances. WeekFields provides five fields, weekOfYear(), weekOfMonth(), dayOfWeek(), weekOfWeekBasedYear(), and weekBasedYear() that provide access to the values from any temporal object.
A separate Field instance is required for each different WeekFields and requires:
- Start of week
- Minimum number of days
Class declaration:
public final class WeekFields extends Object implements Serializable
WeekFields class inherits following methods from class java.lang.Object:
- clone()
- finalize()
- getClass()
- notify()
- notifyAll()
- wait()
Method of WeekFields Class:
Method | Description |
---|---|
dayOfWeek() | This method returns a field to access the day of the week based on this WeekFields. |
equals(Object object) | This method checks if this WeekFields is equal to the specified object. |
getFirstDayOfWeek() | This method gets the first day-of-week. |
getMinimalDaysInFirstWeek() | This method gets a minimal number of days in the first week. |
hashCode() | This method returns a hash code for this WeekFields. |
of(DayOfWeek firstDayOfWeek, int minimalDaysInFirstWeek) | This method obtains an instance of WeekFields from the first day-of-week and minimal days. |
of(Locale locale) | This method obtains an instance of WeekFields appropriate for a locale. |
toString() | This method gets a string representation of this WeekFields instance. |
weekBasedYear() | This method returns a field to access the year of a week-based-year based on this WeekFields. |
weekOfMonth() | This method returns a field to access the week of month based on this WeekFields. |
weekOfWeekBasedYear() | This method returns a field to access the week of a week-based-year based on this WeekFields. |
weekOfYear() | This method returns a field to access the week of year based on this WeekFields. |
Example 1:
Java
// Java program to demonstrate // WeekFields class 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 ); // creating separate temporal fields for each method // apply dayOfWeek() TemporalField dayOfWeek = weekFields.dayOfWeek(); // apply weekBasedYear() TemporalField weekBasedYear = weekFields.weekBasedYear(); // apply weekOfMonth() TemporalField weekOfMonth = weekFields.weekOfMonth(); // apply weekOfWeekBasedYear() TemporalField weekOfWeekBasedYear = weekFields.weekOfWeekBasedYear(); // create a LocalDate LocalDate day = LocalDate.of( 2021 , 03 , 31 ); // get day of week for localdate int dow = day.get(dayOfWeek); // get week based year for localdate int wby = day.get(weekBasedYear); // get week of month for localdate int wom = day.get(weekOfMonth); // get week of week for localdate int wow = day.get(weekOfWeekBasedYear); // print results System.out.println( "day of week for " + day + " :" + dow); System.out.println( "week based year for " + day + " :" + wby); System.out.println( "week of month for " + day + " :" + wom); System.out.println( "Week of week for " + day + " :" + wow); } } |
day of week for 2021-03-31 :3 week based year for 2021-03-31 :2021 week of month for 2021-03-31 :5 Week of week for 2021-03-31 :14
Example 2:
Java
// Java program to demonstrate // WeekFields class 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 ); // creating separate temporal fields for each method // apply dayOfWeek() TemporalField dayOfWeek = weekFields.dayOfWeek(); // apply weekBasedYear() TemporalField weekBasedYear = weekFields.weekBasedYear(); // apply weekOfMonth() TemporalField weekOfMonth = weekFields.weekOfMonth(); // apply weekOfWeekBasedYear() TemporalField weekOfWeekBasedYear = weekFields.weekOfWeekBasedYear(); // create a LocalDate LocalDate day = LocalDate.of( 2021 , 12 , 05 ); // get day of week for localdate int dow = day.get(dayOfWeek); // get week based year for localdate int wby = day.get(weekBasedYear); // get week of month for localdate int wom = day.get(weekOfMonth); // get week of week for localdate int wow = day.get(weekOfWeekBasedYear); // print results System.out.println( "day of week for " + day + " :" + dow); System.out.println( "week based year for " + day + " :" + wby); System.out.println( "week of month for " + day + " :" + wom); System.out.println( "Week of week for " + day + " :" + wow); } } |
day of week for 2021-12-05 :1 week based year for 2021-12-05 :2021 week of month for 2021-12-05 :2 Week of week for 2021-12-05 :50