LocalDateTime类的getYear()方法用于返回year字段。此方法将MIN_YEAR到MAX_YEAR的Year的原始int值返回。
用法:
public int getYear()
参数:此方法不接受任何参数。
返回值:此方法返回一个整数值,该整数值是从MIN_YEAR到MAX_YEAR的年份的原始int值。
以下示例程序旨在说明LocalDateTime.getYear()方法:
示例1:
// Java program to demonstrate
// LocalDateTime.getYear() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDateTime Object
LocalDateTime local
= LocalDateTime.parse("2018-12-03T12:39:10");
// get Year
int year = local.getYear();
// print result
System.out.println("Year: "
+ year);
}
}
输出:
Year: 2018
示例2:
// Java program to demonstrate
// LocalDateTime.getYear() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDateTime Object
LocalDateTime local
= LocalDateTime.parse("2019-01-28T22:29:10");
// get Year
int year = local.getYear();
// print result
System.out.println("Year: "
+ year);
}
}
输出:
Year: 2019
参考:https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#getYear()