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



說明

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

宣告

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

public OffsetDateTime with(TemporalField field, long newValue)

引數

  • field − 要在結果中設定的欄位,非空。

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

返回值

基於此日期時間並進行調整的 OffsetDateTime,非空。

異常

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

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

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

示例

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

package com.tutorialspoint;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoField;

public class OffsetDateTimeDemo {
   public static void main(String[] args) {
      
      OffsetDateTime date = OffsetDateTime.parse("2017-01-03T10:15:30+01:00");
      OffsetDateTime result = date.with(ChronoField.DAY_OF_MONTH,13);
      System.out.println(result);  
   }
}

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

2017-07-31T10:15:30+01:00
廣告
© . All rights reserved.