YearMonth withYear() method in Java with Examples
withYear(int year) method of the YearMonth class used to alter the year of YearMonth object using year passed as a parameter and after that method returns the copy of altered YearMonth. This instance is immutable and unaffected by this method call.
Syntax:
public YearMonth withYear(int year)
Parameters: This method accepts month as parameter which is the year to set in the returned year-month, from MIN_YEAR to MAX_YEAR.
Return value: This method returns a YearMonth based on this year-month with the requested year.
Exception: This method throws DateTimeException if the year value is invalid.
Below programs illustrate the withYear() method:
Program 1:
// Java program to demonstrate // YearMonth.withYear() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a YearMonth object YearMonth yr = YearMonth.of( 2019 , 12 ); // print instance System.out.println( "YearMonth before" + " applying method: " + yr); // apply withYear method of YearMonth class YearMonth updatedlocal = yr.withYear( 2023 ); // print instance System.out.println( "YearMonth after" + " applying method: " + updatedlocal); } } |
YearMonth before applying method: 2019-12 YearMonth after applying method: 2023-12
Program 2:
// Java program to demonstrate // YearMonth.withYear() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a YearMonth object YearMonth yr = YearMonth.of( 2022 , 7 ); // print instance System.out.println( "YearMonth before" + " applying method: " + yr); // apply withYear method of YearMonth class YearMonth updatedlocal = yr.withYear( 1992 ); // print instance System.out.println( "YearMonth after" + " applying method: " + updatedlocal); } } |
YearMonth before applying method: 2022-07 YearMonth after applying method: 1992-07
References: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#withYear(int)
Recommended Posts:
- ZonedDateTime withYear() method in Java with Examples
- OffsetDateTime withYear() method in Java with examples
- LocalDateTime withYear() method in Java with Examples
- LocalDate withYear() method in Java with Examples
- YearMonth until() Method in Java with Examples
- YearMonth with() Method in Java with Examples
- YearMonth plus(TemporalAmount) method in Java with Examples
- YearMonth lengthOfYear() method in Java with Examples
- YearMonth isValidDay() method in Java with Examples
- YearMonth query() Method in Java with Examples
- YearMonth isBefore() method in Java with Examples
- YearMonth isAfter() method in Java with Examples
- YearMonth range() method in Java with Examples
- YearMonth isLeapYear() method in Java with Examples
- YearMonth withMonth() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.