java.time.OffsetDateTime.plusYears() 方法示例



描述

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.tutorialspoint;

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
廣告
© . All rights reserved.