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