Java中的YearMonth类的getYear()方法用于从与其一起使用的Yearyearth实例中获取Year字段的值。它以从MIN_YEAR到MAX_YEAR的整数形式返回Year字段的值。
用法:
public int getYear()
参数:此方法不接受任何参数。
返回值:它将年份字段的值作为整数从MIN_YEAR返回到MAX_YEAR。
以下程序说明了Java中YearMonth的getYear()方法:
示例1::
// Program to illustrate the getYear() 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 year field
System.out.println(thisYearMonth.getYear());
}
}
输出:
2017
示例2::
// Program to illustrate the getYear() 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 year field
System.out.println(thisYearMonth.getYear());
}
}
输出:
2018