Thursday, September 18, 2025
HomeLanguagesJavaChronoLocalDateTime with(TemporalAdjuster) method in Java with Examples

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.

Syntax:

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:

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(Month.OCTOBER)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("ChronoLocalDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}


Output:

ChronoLocalDateTime before adjustment: 2019-12-31T19:15:30
ChronoLocalDateTime after adjustment: 2019-10-01T19: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(Month.JANUARY)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("ChronoLocalDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}


Output:

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

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

RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6660 POSTS0 COMMENTS
Nicole Veronica
11834 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7052 POSTS0 COMMENTS
Thapelo Manthata
6735 POSTS0 COMMENTS
Umr Jansen
6741 POSTS0 COMMENTS