Java中的 WeekFields getMinimalDaysInFirstWeek() 方法及示例
WeekFields 类的getMinimalDaysInFirstWeek()方法用于返回一个月或一年第一周的最少天数。例如,ISO-8601 要求在计算第一周之前存在 4 天(超过半周)。此方法返回一个月或一年第一周的最小天数,从 1 到 7。
句法:
public int getMinimalDaysInFirstWeek()
参数:此方法不接受任何内容。
返回值:该方法返回一个月或一年第一周的最小天数,从 1 到 7。
下面的程序说明了 WeekFields.getMinimalDaysInFirstWeek() 方法:
方案一:
// Java program to demonstrate
// WeekFields.getMinimalDaysInFirstWeek() method
import java.time.DayOfWeek;
import java.time.temporal.WeekFields;
public class GFG {
public static void main(String[] args)
{
// create WeekFields
WeekFields weekFields
= WeekFields.of(DayOfWeek.SATURDAY, 2);
// apply getMinimalDaysInFirstWeek()
int noOfDays
= weekFields.getMinimalDaysInFirstWeek();
// print results
System.out.println("Minimal number of days"
+ " in the first week: "
+ noOfDays);
}
}
输出:
Minimal number of days in the first week: 2
方案二:
// Java program to demonstrate
// WeekFields.getMinimalDaysInFirstWeek() method
import java.time.temporal.WeekFields;
import java.util.Locale;
public class GFG {
public static void main(String[] args)
{
Locale locale = new Locale("EN", "INDIA");
// create WeekFields
WeekFields weekFields
= WeekFields.of(locale);
// apply getMinimalDaysInFirstWeek()
int noOfDays
= weekFields.getMinimalDaysInFirstWeek();
// print results
System.out.println("Minimal number of days"
+ " in the first week: "
+ noOfDays);
}
}
输出:
Minimal number of days in the first week: 1
参考资料:https: Java/time/temporal/WeekFields.html#getMinimalDaysInFirstWeek()