YearMonth withYear() method in Java with Examples Last Updated : 15 Jan, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output: YearMonth before applying method: 2019-12 YearMonth after applying method: 2023-12 Program 2: Java // 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); } } Output: 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) Comment More infoAdvertise with us Next Article Year with() Method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-time package Java-YearMonth Practice Tags : Java Similar Reads YearMonth with() Method in Java with Examples In YearMonth class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the YearMonth class used to adjusted this YearMonth using TemporalAdjuster and after adjustment returns the copy of adjusted 3 min read YearMonth withMonth() method in Java with Examples withMonth(int month) method of the YearMonth class used to alter the month-of-year of YearMonth object using month passed as a parameter and after that method returns the copy of altered YearMonth.If the day-of-month is invalid for the specified month, the day will be adjusted to the last valid day- 2 min read YearMonth now() method in Java with Examples The now() method of the YearMonth class in Java is used to obtain the current year-month from the system clock in the default time-zone. Syntax: public static YearMonth now() Parameters: This method does not accept any parameter. Return value: This method returns the current year-month using the sys 1 min read Year with() Method in Java with Examples In Year class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the Year class used to adjusted this Year using TemporalAdjuster and after adjustment returns the copy of adjusted Year.The adjust 3 min read YearMonth until() Method in Java with Examples until() method of the YearMonth class used to calculate the amount of time between two YearMonth objects using TemporalUnit. The start and end points are this and the specified YearMonth passed as a parameter. The result will be negative if the end is before the start. The calculation returns a whol 2 min read YearMonth minusMonths() method in Java with Examples The minusMonths() method of YearMonth class in Java is used to return a copy of this YearMonth with the specified number of months subtracted. Syntax: public YearMonth minusMonths( long monthsToSubtract) Parameter: This method accepts monthsToSubtract as parameters which represents months to subtrac 2 min read Like