Open In App

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 YearMonth.The adjustment takes place using the specified adjuster strategy object. This instance of YearMonth is immutable and unaffected by this method call.



Syntax:

public YearMonth with(TemporalAdjuster adjuster)

Parameters: This method accepts adjuster as parameter which is the adjuster to use.



Return value: This method returns a YearMonth based on this with the adjustment made.

Exception: This method throws following Exceptions:

Below programs illustrate the with() method:
Program 1:




// Java program to demonstrate
// YearMonth.with() 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"
                           + " adjustment: "
                           + yr);
  
        // apply with method of YearMonth class
        YearMonth updatedlocal
            = yr.with(YearMonth.of(2012, 12));
  
        // print instance
        System.out.println("YearMonth after"
                           + " adjustment: "
                           + updatedlocal);
    }
}

Output:
YearMonth before adjustment: 2019-12
YearMonth after adjustment: 2012-12

with(TemporalField field, long newValue)

with(TemporalField field, long newValue) method of the YearMonth class used to set the specified field of YearMonth to a new value and returns the copy of new time.This method can be used to change any supported field, such as year or month. An exception is thrown If setting the new value is not possible due to the field is not supported or for some other reason. This instance of YearMonth is immutable and unaffected by this method call.

Syntax:

public YearMonth with(TemporalField field, long newValue)

Parameters: This method accepts field which is the field to set in the result and newValue which the new value of the field in the result as parameters.

Return value: This method returns a YearMonth based on this with the specified field set.

Exception: This method throws following Exceptions:

Below programs illustrate the with() method:
Program 1:




// Java program to demonstrate
// YearMonth.with() 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 with method of YearMonth class
        YearMonth updatedlocal
            = yr.with(ChronoField.YEAR, 2100);
  
        // print instance
        System.out.println("YearMonth after"
                           + " applying method: "
                           + updatedlocal);
    }
}

Output:
YearMonth before applying method: 2019-12
YearMonth after applying method: 2100-12

References:
https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#with(java.time.temporal.TemporalAdjuster)
https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#with(java.time.temporal.TemporalField, long)


Article Tags :