Java的.time.temporal.WeekFields类在Java中
WeekFields类表示周的定义,以提供 TemporalField 实例。 WeekFields 提供了五个字段,weekOfYear()、weekOfMonth()、dayOfWeek()、weekOfWeekBasedYear() 和 weekBasedYear(),它们提供对来自任何时间对象的值的访问。
每个不同的 WeekField 都需要一个单独的 Field 实例,并且需要:
- 一周开始
- 最少天数
类声明:
public final class WeekFields
extends Object
implements Serializable
WeekFields 类从类Java.lang.Object继承以下方法:
- 克隆()
- 完成()
- 获取类()
- 通知()
- 通知所有()
- 等待()
WeekFields 类的方法:
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. |
示例 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);
}
}
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-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
示例 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