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

The with(TemporalField field, long newValue) method of the ChronoZonedDateTime interface is used to set the specified field of ChronoZonedDateTime 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 ChronoZonedDateTime is immutable and unaffected by this method call.

Syntax:

ChronoZonedDateTime with(TemporalField field, 
                         long newValue)

Parameters: This method accepts two parameters:

  • 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 ChronoZonedDateTime 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:

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(ChronoField.YEAR, 2017);
  
        // 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: 2017-12-06T19:21:12.123+05:30[Asia/Calcutta]

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(
                      "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(ChronoField.MONTH_OF_YEAR, 1);
  
        // 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-01-25T23:12:38.543Z[Europe/Paris]

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

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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.