Open In App

ZonedDateTime with() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

In ZonedDateTime class, there are two types of with() method depending upon the parameters passed to it.

with(TemporalAdjuster adjuster)

with(TemporalAdjuster adjuster) method of the ZonedDateTime class used to adjusted this date-time and after adjustment returns the copy of adjusted date-time.The adjustment takes place using the specified adjuster strategy object. This instance of ZonedDateTime is immutable and unaffected by this method call.

Syntax:

public ZonedDateTime with(TemporalAdjuster adjuster)

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

Return value: This method returns a ZonedDateTime 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
// ZonedDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zoneddatetime
            = ZonedDateTime.parse(
                "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // print instance
        System.out.println("ZonedDateTime before"
                           + " adjustment: "
                           + zoneddatetime);
  
        // apply with method of ZonedDateTime class
        ZonedDateTime zt
            = zoneddatetime
                  .with(Month.SEPTEMBER)
                  .with(TemporalAdjusters.firstDayOfMonth());
  
        // print instance
        System.out.println("ZonedDateTime after"
                           + " adjustment: "
                           + zt);
    }
}


Output:

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

with(TemporalField field, long newValue)

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

Syntax:

public ZonedDateTime 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 ZonedDateTime 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
// ZonedDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zoneddatetime
            = ZonedDateTime.parse(
                "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // print instance
        System.out.println("ZonedDateTime before"
                           + " applying method: "
                           + zoneddatetime);
  
        // apply with method of ZonedDateTime class
        ZonedDateTime zt
            = zoneddatetime.with(
                ChronoField.HOUR_OF_DAY, 13);
  
        // print instance
        System.out.println("ZonedDateTime after"
                           + " applying method: "
                           + zt);
    }
}


Output:

ZonedDateTime before applying method: 2018-12-06T19:21:12.123+05:30[Asia/Calcutta]
ZonedDateTime after applying method: 2018-12-06T13:21:12.123+05:30[Asia/Calcutta]

References:
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#with(java.time.temporal.TemporalField, long)
https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#with(java.time.temporal.TemporalAdjuster)



Last Updated : 11 Jan, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads