Thursday, July 4, 2024
HomeLanguagesJavaOffsetDateTime with() Method in Java with Examples

OffsetDateTime with() Method in Java with Examples

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

with(TemporalAdjuster adjuster)

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

Syntax:

public OffsetDateTime with(TemporalAdjuster adjuster)

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

Return value: This method returns a OffsetDateTime 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
// OffsetDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetDateTime object
        OffsetDateTime off
            = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
  
        // print instance
        System.out.println("OffsetDateTime before"
                           + " adjustment: "
                           + off);
  
        // apply with method of OffsetDateTime class
        OffsetDateTime updatedlocal
            = off.with(Month.JUNE)
                  .with(TemporalAdjusters
                            .firstDayOfMonth());
  
        // print instance
        System.out.println("OffsetDateTime after"
                           + " adjustment: "
                           + updatedlocal);
    }
}


Output:

OffsetDateTime before adjustment: 2018-12-12T13:30:30+05:00
OffsetDateTime after adjustment: 2018-06-01T13:30:30+05:00

with(TemporalField field, long newValue)

with(TemporalField field, long newValue) method of the OffsetDateTime class used to set the specified field of OffsetDateTime to a new value and returns the copy of new time.This method can be used to change any supported field, such as 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. This instance of OffsetDateTime is immutable and unaffected by this method call.

Syntax:

public OffsetDateTime 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 OffsetDateTime 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
// OffsetDateTime.with() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a OffsetDateTime object
        OffsetDateTime off
            = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
  
        // print instance
        System.out.println("OffsetDateTime before"
                           + " adjustment: "
                           + off);
  
        // apply with method of OffsetDateTime class
        OffsetDateTime updatedlocal
            = off.with(ChronoField.DAY_OF_MONTH, 31);
  
        // print instance
        System.out.println("OffsetDateTime after"
                           + " applying method: "
                           + updatedlocal);
    }
}


Output:

OffsetDateTime before adjustment: 2018-12-12T13:30:30+05:00
OffsetDateTime after applying method: 2018-12-31T13:30:30+05:00

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments