Java中的YearMonth类的getMonth()方法用于从与其一起使用的Yearyearth实例中获取month字段。它返回month字段作为Month实例。
用法:
public Month getMonth()
参数:此方法不接受任何参数。
返回值:它将返回此YearMonth实例的month字段。它不返回值,而是返回Month实例。
以下程序说明了Java中YearMonth的getMonth()方法:
示例1::
// Program to illustrate the getMonth() method
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GfG {
public static void main(String[] args)
{
// Create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2017, 8);
// Get the month field
System.out.println(thisYearMonth.getMonth());
}
}
输出:
AUGUST
示例2::
// Program to illustrate the getMonth() method
import java.util.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class GfG {
public static void main(String[] args)
{
// Create a YearMonth object
YearMonth thisYearMonth = YearMonth.of(2018, 5);
// Get the month field
System.out.println(thisYearMonth.getMonth());
}
}
输出:
MAY