Skip to content
Related Articles

Related Articles

ChronoLocalDateTime with(TemporalField, long) method in Java with Examples

Improve Article
Save Article
  • Last Updated : 28 May, 2019
Improve Article
Save Article

The with(TemporalField field, long newValue) method of the ChronoLocalDateTime interface is used to set the specified field of ChronoLocalDateTime to a new value and returns the copy of new time. This method can be used to change any supported field, such as year, day, month, hour, minute or second. 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 ChronoLocalDateTime is immutable and unaffected by this method call.

Syntax:

ChronoLocalDateTime 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 ChronoLocalDateTime based on this with the specified field set.

Exception: This method throws following Exceptions:

  • DateTimeException – if the adjustment cannot be made.
  • UnsupportedTemporalTypeException – if the field is not supported.
  • ArithmeticException – if numeric overflow occurs.

Below programs illustrate the with() method:

Program 1:




// Java program to demonstrate
// ChronoLocalDateTime.with() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ChronoLocalDateTime object
        ChronoLocalDateTime time
            = LocalDateTime
                  .parse("2019-12-31T19:15:30");
  
        // print instance
        System.out.println("ChronoLocalDateTime before"
                           + " adjustment: "
                           + time);
  
        // apply with method of ChronoLocalDateTime
        ChronoLocalDateTime updatedlocal
            = time.with(ChronoField.YEAR, 2017);
  
        // print instance
        System.out.println("ChronoLocalDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}

Output:

ChronoLocalDateTime before adjustment: 2019-12-31T19:15:30
ChronoLocalDateTime after adjustment: 2017-12-31T19:15:30

Program 2:




// Java program to demonstrate
// ChronoLocalDateTime.with() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ChronoLocalDateTime object
        ChronoLocalDateTime time
            = LocalDateTime
                  .parse("2018-10-25T23:12:31.123");
  
        // print instance
        System.out.println("ChronoLocalDateTime before"
                           + " adjustment: "
                           + time);
  
        // apply with method of ChronoLocalDateTime
        ChronoLocalDateTime updatedlocal
            = time.with(ChronoField.MONTH_OF_YEAR, 1);
  
        // print instance
        System.out.println("ChronoLocalDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}

Output:

ChronoLocalDateTime before adjustment: 2018-10-25T23:12:31.123
ChronoLocalDateTime after adjustment: 2018-01-25T23:12:31.123

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/chrono/ChronoLocalDateTime.html#with-java.time.temporal.TemporalField-long-


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!