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