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("-12-03T12:39:10");
// get Year
int year = local.getYear();
// print result
System.out.println("Year: "
+ year);
}
}
输出:
Year:
示例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("-01-28T22:29:10");
// get Year
int year = local.getYear();
// print result
System.out.println("Year: "
+ year);
}
}
输出:
Year:
参考:/javase/10/docs/api/java/time/LocalDateTime.html#getYear()