Java中的 OffsetDateTime getYear() 方法及示例
Java中OffsetDateTime类的getYear()方法用于获取年份字段的值。
句法 :
public int getYear()
参数:此方法不接受任何参数。
返回值:返回年份,从 MIN_YEAR 到 MAX_YEAR。
下面的程序说明了getYear()方法:
程序 1:
// Java program to demonstrate the getYear() method
import java.time.OffsetDateTime;
public class GFG {
public static void main(String[] args)
{
// parses a date
OffsetDateTime date = OffsetDateTime.parse("2018-12-03T12:30:30+01:00");
// Prints the year of given date
System.out.println("Year: " + date.getYear());
}
}
输出:
Year: 2018
方案二:
// Java program to demonstrate the getYear() method
import java.time.OffsetDateTime;
public class GFG {
public static void main(String[] args)
{
// parses a date
OffsetDateTime date = OffsetDateTime.parse("2016-10-03T12:30:30+01:20");
// Prints the year of given date
System.out.println("Year: " + date.getYear());
}
}
输出:
Year: 2016
参考:https: Java/time/OffsetDateTime.html#getYear–