ChronoZonedDateTime with(TemporalAdjuster) method in Java with Examples

The with(TemporalAdjuster adjuster) method of the ChronoZonedDateTime 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 ChronoZonedDateTime 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 ChronoZonedDateTime with(TemporalAdjuster adjuster)

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

Return value: This method returns a ChronoZonedDateTime 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ChronoZonedDateTime.with() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ChronoZonedDateTime object
        ChronoZonedDateTime time
            = ZonedDateTime
                  .parse(
                      "1918-10-25T23:12:38.543+02:00[Europe/Paris]");
  
        // print instance
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time);
  
        // apply with method of ChronoZonedDateTime
        ChronoZonedDateTime updatedlocal
            = time.with(Month.OCTOBER)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}

chevron_right


Output:

ChronoZonedDateTime before adjustment: 1918-10-25T23:12:38.543Z[Europe/Paris]
ChronoZonedDateTime after adjustment: 1918-10-01T23:12:38.543+01:00[Europe/Paris]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// ChronoZonedDateTime.with() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create ChronoZonedDateTime object
        ChronoZonedDateTime time
            = ZonedDateTime
                  .parse(
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // print instance
        System.out.println("ChronoZonedDateTime before"
                           + " adjustment: "
                           + time);
  
        // apply with method of ChronoZonedDateTime
        ChronoZonedDateTime updatedlocal
            = time.with(Month.JANUARY)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("ChronoZonedDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}

chevron_right


Output:

ChronoZonedDateTime before adjustment: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ChronoZonedDateTime after adjustment: 2018-01-01T19:21:12.123+05:30[Asia/Calcutta]

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts: