java.time.Year.adjustInto() 方法示例



說明

java.time.Year.adjustInto(Temporal temporal) 方法調整指定的時間物件使之具有當前年份。

宣告

以下是 java.time.Year.adjustInto(TemporalType temporal) 方法的宣告。

public Temporal adjustInto(Temporal temporal)

引數

temporal − 要調整的時間物件,不能為空。

返回值

具有已做調整的相同型別的物件,不能為空。

異常

  • DateTimeException − 如果無法新增。

  • ArithmeticException − 如果發生數字溢位。

示例

以下示例展示了 java.time.Year.adjustInto(TemporalType temporal) 方法的用法。

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.Year;

public class YearDemo {
   public static void main(String[] args) {

      Year year = Year.of(2015);
      
      LocalDate date = LocalDate.now();
      System.out.println(date);  

      date = (LocalDate)year.adjustInto(date);
      System.out.println(date);  
   }
}

讓我們編譯並執行以上程式,這會產生以下結果 −

2017-04-01
2015-04-01
廣告
© . All rights reserved.