Java中的OffsetDateTime類的getYear()方法用於獲取Year字段的值。
用法:
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
程序2:
// 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://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html#getYear–
相關用法
- Java ZonedDateTime getYear()用法及代碼示例
- Java LocalDate getYear()用法及代碼示例
- Java LocalDateTime getYear()用法及代碼示例
- Java OffsetDateTime until()用法及代碼示例
- Java OffsetDateTime with()用法及代碼示例
- Java OffsetDateTime get()用法及代碼示例
- Java OffsetDateTime from()用法及代碼示例
- Java OffsetDateTime withHour()用法及代碼示例
- Java OffsetDateTime range()用法及代碼示例
- Java OffsetDateTime plusMinutes()用法及代碼示例
- Java OffsetDateTime compareTo()用法及代碼示例
- Java OffsetDateTime minusWeeks()用法及代碼示例
- Java OffsetDateTime getLong()用法及代碼示例
- Java OffsetDateTime minusMinutes()用法及代碼示例
- Java OffsetDateTime withSecond()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 OffsetDateTime getYear() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。