Java month firstDayOfYear()方法

来源:这里教程网 时间:2026-02-17 20:57:41 作者:

Java month firstDayOfYear()方法

firstDayOfYear() 是Month ENUM的一个内置方法,用于获取与该月第一天相对应的一年中的哪一天。

语法:

public int firstDayOfYear(boolean leapYear)

参数 :该方法接受一个参数leapYear,它是一个布尔标志变量,表示这一年是否是闰年。

返回值 :该方法返回本月第一天所对应的年月日。

下面的程序说明了上述方法。

程序1 :

import java.time.*;import java.time.Month;import java.time.temporal.Temporal;  class DayOfWeekExample {    public static void main(String[] args)    {        // Set the month to february        Month month = Month.of(2);          // Get corresponding day-of-year of the        // first day of february in a non-leap year        System.out.println(month.firstDayOfYear(false));    }}

输出:

32

程序2 :

import java.time.*;import java.time.Month;import java.time.temporal.Temporal;  class DayOfWeekExample {    public static void main(String[] args)    {        // Set the month to february        Month month = Month.of(3);          // Get corresponding day-of-year of the        // first day of March in a leap year        System.out.println(month.firstDayOfYear(true));    }}

输出:

61

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/Month.html#firstDayOfYear-boolean-

相关推荐