java.time.temporal.WeekFields Class in Java
Last Updated :
17 Mar, 2021
Improve
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 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);
}
}
60
1
// Java program to demonstrate
2
// WeekFields class
3
4
import java.time.DayOfWeek;
5
import java.time.LocalDate;
6
import java.time.temporal.TemporalField;
7
import java.time.temporal.WeekFields;
8
9
public class GFG {
10
public static void main(String[] args)
11
{
12
13
// create WeekFields
14
WeekFields weekFields
15
= WeekFields.of(DayOfWeek.MONDAY, 1);
16
17
// creating separate temporal fields for each method
18
19
// apply dayOfWeek()
20
TemporalField dayOfWeek = weekFields.dayOfWeek();
21
22
// apply weekBasedYear()
23
TemporalField weekBasedYear
24
= weekFields.weekBasedYear();
25
26
// apply weekOfMonth()
27
TemporalField weekOfMonth
28
= weekFields.weekOfMonth();
29
30
// apply weekOfWeekBasedYear()
31
TemporalField weekOfWeekBasedYear
32
= weekFields.weekOfWeekBasedYear();
33
34
// create a LocalDate
35
LocalDate day = LocalDate.of(2021, 03, 31);
36
37
// get day of week for localdate
38
int dow = day.get(dayOfWeek);
39
40
// get week based year for localdate
41
int wby = day.get(weekBasedYear);
42
43
// get week of month for localdate
44
int wom = day.get(weekOfMonth);
45
46
// get week of week for localdate
47
int wow = day.get(weekOfWeekBasedYear);
48
49
// print results
50
51
System.out.println("day of week for " + day + " :"
52
+ dow);
53
System.out.println("week based year for " + day
54
+ " :" + wby);
55
System.out.println("week of month for " + day + " :"
56
+ wom);
57
System.out.println("Week of week for " + day + " :"
58
+ wow);
59
}
60
}
Output
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 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);
}
}
60
1
// Java program to demonstrate
2
// WeekFields class
3
4
import java.time.DayOfWeek;
5
import java.time.LocalDate;
6
import java.time.temporal.TemporalField;
7
import java.time.temporal.WeekFields;
8
9
public class GFG {
10
public static void main(String[] args)
11
{
12
13
// create WeekFields
14
WeekFields weekFields
15
= WeekFields.of(DayOfWeek.SUNDAY, 1);
16
17
// creating separate temporal fields for each method
18
19
// apply dayOfWeek()
20
TemporalField dayOfWeek = weekFields.dayOfWeek();
21
22
// apply weekBasedYear()
23
TemporalField weekBasedYear
24
= weekFields.weekBasedYear();
25
26
// apply weekOfMonth()
27
TemporalField weekOfMonth
28
= weekFields.weekOfMonth();
29
30
// apply weekOfWeekBasedYear()
31
TemporalField weekOfWeekBasedYear
32
= weekFields.weekOfWeekBasedYear();
33
34
// create a LocalDate
35
LocalDate day = LocalDate.of(2021, 12, 05);
36
37
// get day of week for localdate
38
int dow = day.get(dayOfWeek);
39
40
// get week based year for localdate
41
int wby = day.get(weekBasedYear);
42
43
// get week of month for localdate
44
int wom = day.get(weekOfMonth);
45
46
// get week of week for localdate
47
int wow = day.get(weekOfWeekBasedYear);
48
49
// print results
50
51
System.out.println("day of week for " + day + " :"
52
+ dow);
53
System.out.println("week based year for " + day
54
+ " :" + wby);
55
System.out.println("week of month for " + day + " :"
56
+ wom);
57
System.out.println("Week of week for " + day + " :"
58
+ wow);
59
}
60
}
Output
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