Returns a copy of this year-month with the specified field set to the specified new value.
package com.logicbig.example.yearmonth;
import java.time.YearMonth;
import java.time.temporal.ChronoField;
public class WithExample2 {
public static void main(String... args) {
YearMonth y = YearMonth.of(2025, 5);
System.out.println(y);
for (ChronoField chronoField : ChronoField.values()) {
if (y.isSupported(chronoField)) {
YearMonth y2 = y.with(chronoField, 1);
System.out.printf("%15s => %s%n", chronoField.name(), y2);
}
}
}
}
Output
2025-05
MONTH_OF_YEAR => 2025-01
PROLEPTIC_MONTH => 0000-02
YEAR_OF_ERA => 0001-05
YEAR => 0001-05
ERA => 2025-05