ChronoLocalDateTime with(TemporalAdjuster) method in Java with Examples
The with(TemporalAdjuster adjuster) method of the ChronoLocalDateTime interface is used to adjust this date-time using TemporalAdjuster and after adjustment returns the copy of adjusted date-time. The adjustment takes place using the specified adjuster strategy object. This instance of ChronoLocalDateTime is immutable and unaffected by this method call. A simple adjuster uses to set one of the fields, such as the year field where A more complex adjuster might set the time to the last day of the year.
default ChronoLocalDateTime with(TemporalAdjuster adjuster)
Parameters: This method accepts adjuster as parameter which is the adjuster to use.
Return value: This method returns a ChronoLocalDateTime based on this with the adjustment made.
Exception: This method throws following Exceptions:
- DateTimeException – if the adjustment cannot be made.
- ArithmeticException – if numeric overflow occurs.
Below programs illustrate the with() method:
ChronoLocalDateTime before adjustment: 2019-12-31T19:15:30 ChronoLocalDateTime after adjustment: 2019-10-01T19:15:30
ChronoLocalDateTime before adjustment: 2018-10-25T23:12:31.123 ChronoLocalDateTime after adjustment: 2018-01-01T23:12:31.123
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.