Wednesday, July 3, 2024
HomeLanguagesJavaZonedDateTime plus() method in Java with Examples

ZonedDateTime plus() method in Java with Examples

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

plus(long amountToAdd, TemporalUnit unit)

plus() method of a ZonedDateTime class used to return a copy of this date-time with the specified amount of unit added.If it is not possible to add the amount, because the unit is not supported or for some other reason, an exception is thrown.

Syntax:

public ZonedDateTime plus(long amountToAdd,
                          TemporalUnit unit)

Parameters: This method accepts two parameters:

  • amountToAdd: which is the amount of the unit to add to the result, may be negative
  • unit: which is the unit of the amount to add.

Return value: This method returns ZonedDateTime based on this date-time with the specified amount added.

Exception: This method throws following Exceptions:

  • DateTimeException: if the addition cannot be made.
  • UnsupportedTemporalTypeException: if the unit is not supported.
  • ArithmeticException: if numeric overflow occurs.

Below programs illustrate the plus() method:

Program 1:




// Java program to demonstrate
// ZonedDateTime.plus() method
  
import java.time.*;
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedlt
            = ZonedDateTime
                  .parse(
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // add 30 Months to ZonedDateTime
        ZonedDateTime value
            = zonedlt.plus(30, ChronoUnit.MONTHS);
  
        // print result
        System.out.println("ZonedDateTime after"
                           + " adding Months: \n"
                           + value);
    }
}


Output:

ZonedDateTime after adding Months: 
2021-06-06T19:21:12.123+05:30[Asia/Calcutta]

plus(TemporalAmount amountToAdd)

plus() method of a ZonedDateTime class used to return a copy of this date-time with the specified amount added to date-time.The amount is typically Period or Duration but may be any other type implementing the TemporalAmount interface.

Syntax:

public ZonedDateTime plus(TemporalAmount amountToAdd)

Parameters: This method accepts one single parameter amountToAdd which is the amount to add, It should not be null.

Return value: This method returns ZonedDateTime based on this date-time with the addition made.

Exception: This method throws following Exceptions:

  • DateTimeException: if the addition cannot be made.
  • ArithmeticException: if numeric overflow occurs.

Below programs illustrate the plus() method:

Program 1:




// Java program to demonstrate
// ZonedDateTime.plus() method
  
import java.time.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedlt
            = ZonedDateTime
                  .parse(
                      "2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
  
        // add 100 Days to ZonedDateTime
        ZonedDateTime value
            = zonedlt.plus(Period.ofDays(100));
  
        // print result
        System.out.println("ZonedDateTime after"
                           + " adding Days: \n"
                           + value);
    }
}


Output:

ZonedDateTime after adding Days: 
2019-03-16T19:21:12.123+05:30[Asia/Calcutta]

Reference:

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