Java中Year类的atMonth(Month)方法将当前year对象与作为参数传递给月份的month结合起来,以创建YearMonth对象。
用法:
public YearMonth atMonth(Month month)
参数:此方法接受单个参数month。使用的是month-of-year。它需要一个有效的Month对象,并且不能为NULL。
返回值:它返回由当前年份对象和作为参数传递给函数的有效月份组成的YearMonth对象。
以下示例程序旨在说明Java中Year的atMonth(Month 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