Java Year atMonth(month)方法

来源:这里教程网 时间:2026-02-17 21:17:56 作者:

Java Year atMonth(month)方法

Java中Year类的atMonth(Month)方法将当前的年对象与作为参数传递给它的月结合起来,创建一个YearMonth对象。

语法:

public YearMonth atMonth(Month month)

参数 :该方法接受一个单一的参数month。它是要使用的年月日。它需要一个有效的月份对象,并且不能为空。

返回值 :它返回一个由当前年份对象和作为参数传递给该函数的有效月份组成的YearMonth对象。

下面的程序说明了Java中year的atMonth(Month)方法:
程序1 :

// Program to illustrate the atMonth(Month) method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Creates a Year object        Year thisYear = Year.of(2017);          // Creates a YearMonth with this        // Year object and Month passed to it        YearMonth yearMonth = thisYear.atMonth(Month.SEPTEMBER);          System.out.println(yearMonth);    }}

输出:

2017-09

程序2 :

// Program to illustrate the atMonth(Month) method  import java.util.*;import java.time.*;  public class GfG {    public static void main(String[] args)    {        // Creates a Year object        Year thisYear = Year.of(2018);          // Creates a YearMonth with this        // Year object and Month passed to it        YearMonth yearMonth = thisYear.atMonth(Month.JANUARY);          System.out.println(yearMonth);    }}

输出:

2018-01

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/Year.html#atMonth-

相关推荐