java.time.LocalDate.with() 方法示例



說明

java.time.LocalDate.with(TemporalField field, long newValue) 方法返回此日期的副本,其中指定欄位的值設定為新的值。

宣告

以下是對 java.time.LocalDate.with(TemporalField field, long newValue) 方法的宣告。

public LocalDate with(TemporalField field, long newValue)

引數

  • field − 在結果中設定欄位,不得為 null。

  • newValue − 結果中欄位的新值。

返回值

基於此項且已進行調整的 LocalDate,不得為 null。

異常

  • DateTimeException - 如果無法進行調整。

  • UnsupportedTemporalTypeException − 如果欄位不受支援。

  • ArithmeticException − 如果出現數字溢位。

示例

以下示例展示了 java.time.LocalDate.with(TemporalField field, long newValue) 方法的用法。

package com.tutorialspoint;

import java.time.LocalDate;
import java.time.temporal.ChronoField;

public class LocalDateDemo {
   public static void main(String[] args) {
      
      LocalDate date = LocalDate.parse("2017-01-03");
      LocalDate result = date.with(ChronoField.DAY_OF_MONTH,13);
      System.out.println(result);  
   }
}

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

2017-01-13
廣告