java.time.ZonedDateTime.with() Method Example



Description

The java.time.ZonedDateTime.with(TemporalAdjuster adjuster) method returns an adjusted copy of this date-time.

Declaration

Following is the declaration for java.time.ZonedDateTime.with(TemporalAdjuster adjuster) method.

public ZonedDateTime with(TemporalAdjuster adjuster)

Parameters

adjuster − the adjuster to use, not null.

Return Value

a ZonedDateTime based on this with the adjustment made, not null.

Exceptions

  • DateTimeException − if the adjustment cannot be made.

  • ArithmeticException − if numeric overflow occurs.

Example

The following example shows the usage of java.time.ZonedDateTime.with(TemporalAdjuster adjuster) method.

package com.tutorialspoint;

import java.time.ZonedDateTime;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;

public class ZonedDateTimeDemo {
   public static void main(String[] args) {
      
      ZonedDateTime date = ZonedDateTime.parse("2017-03-28T12:25:38.492+05:30[Asia/Calcutta]");
      ZonedDateTime result = date.with(Month.JULY).with(TemporalAdjusters.lastDayOfMonth());
      System.out.println(result);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-07-31T12:25:38.492+05:30[Asia/Calcutta]
Advertisements