Wednesday, July 3, 2024
HomeLanguagesJavaChronoLocalDate with(TemporalField, long) Method in Java with Examples

ChronoLocalDate with(TemporalField, long) Method in Java with Examples

with(TemporalField field, long newValue) method of the ChronoLocalDate interface used to set the specified field of ChronoLocalDate to a new value and returns the copy of new date-time.This method can be used to change any supported field, such as the year, month or day-of-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.
In some cases, changing the specified field can cause the resulting date-time to become invalid, such as changing the month from 31st January to February would make the day-of-month invalid. In cases like this, the field is responsible for resolving the date. Typically it will choose the previous valid date, which would be the last valid day of February in this example. This instance of ChronoLocalDate is immutable and unaffected by this method call.

Syntax:

public ChronoLocalDate 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 ChronoLocalDate 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
// ChronoLocalDate.with() method
  
import java.time.*;
import java.time.temporal.*;
import java.time.chrono.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ChronoLocalDate object
        ChronoLocalDate local
            = LocalDate.parse(
                "2018-12-06");
  
        // print instance
        System.out.println("ChronoLocalDate before"
                           + " applying method: "
                           + local);
  
        // apply with method of ChronoLocalDate class
        ChronoLocalDate updatedlocal
            = local.with(
                ChronoField.DAY_OF_MONTH, 30);
  
        // print instance
        System.out.println("ChronoLocalDate after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

ChronoLocalDate before applying method: 2018-12-06
ChronoLocalDate after applying method: 2018-12-30

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments