Javatime-instant-with1
提供:Dev Guides
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.finddevguides;
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