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



說明

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

宣告

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

public OffsetTime with(TemporalField field, long newValue)

引數

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

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

返回值

基於此時間副本,進行調整,不能為空。

異常

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

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

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

示例

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

package com.tutorialspoint;

import java.time.OffsetTime;
import java.time.temporal.ChronoField;

public class OffsetTimeDemo {
   public static void main(String[] args) {
      
      OffsetTime date = OffsetTime.parse("10:15:30+01:00");
      OffsetTime result = date.with(ChronoField.HOUR_OF_DAY,13);
      System.out.println(result);  
   }
}

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

13:15:30+01:00
廣告
© . All rights reserved.