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



描述

java.time.Instant.with(TemporalField field, long newValue) 方法返回此瞬時與指定欄位對應的新值的副本。

宣告

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

public Instant with(TemporalField field, long newValue)

引數

  • field - 要在結果中設定的欄位,非 null。

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

返回值

基於此瞬時,其指定欄位已設定,非 null。

異常

  • DateTimeException - 如果無法設定欄位。

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

  • ArithmeticException - 如果發生數值溢位。

示例

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

package com.tutorialspoint;

import java.time.Instant;
import java.time.temporal.ChronoField;

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

      Instant instant = Instant.parse("2017-12-03T10:15:30.00Z");
      System.out.println(instant.with(ChronoField.NANO_OF_SECOND, 20));
   }
}

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

2017-12-03T10:15:30.000000020Z
廣告
© . All rights reserved.