java.time.OffsetDateTime.plusYears(long yearsToAdd)
方法返回此日期時間的副本,並添加了指定的年份。
聲明
以下是java.time.OffsetDateTime.plusYears(long yearsToAdd)
方法的聲明。
public OffsetDateTime plusYears(long yearsToAdd)
參數
yearsToAdd
- 添加的年份,可能是負值。
返回值
基於此日期時間添加年份的OffsetDateTime
,而不是null
。
異常
DateTimeException
- 如果結果超出支持的日期範圍。
示例
以下示例顯示了java.time.OffsetDateTime.plusYears(long yearsToAdd)
方法的用法。
package com.zaixian;
import java.time.OffsetDateTime;
public class OffsetDateTimeDemo {
public static void main(String[] args) {
OffsetDateTime date = OffsetDateTime.parse("2017-02-03T10:15:30+01:00");
System.out.println(date.plusYears(2));
}
}
編譯並運行上面的程式,這將產生以下結果 -
2019-02-03T10:15:30+01:00