Period plusDays() method in Java with Examples
The plusDays() method of Period class in Java is used to add the days to this period. This method operates only on DAYS and does not affect other two YEAR, MONTH.
Syntax:
public Period plusDays(long daysToAdd)
Parameters: This method accepts a single parameter daysToAdd which is the number of days to be added from the period.
Return Value: It returns a Period based on provided period in the input adding the specified number of days. It must not be null.
Exceptions: It throws a ArithmeticException if numeric overflow occurs.
Below programs illustrate the above method:
Program 1:
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodClass {
static void addDays(Period p1, int daystoAdd)
{
System.out.println(p1.plusDays(daystoAdd));
}
public static void main(String[] args)
{
int year = 4 ;
int months = 11 ;
int days = 10 ;
Period p1 = Period.of(year, months, days);
int daystoAdd = 8 ;
addDays(p1, daystoAdd);
}
}
|
Program 2: Period can be negative.
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class PeriodClass {
static void addDays(Period p1, int daystoAdd)
{
System.out.println(p1.plusDays(daystoAdd));
}
public static void main(String[] args)
{
int year = - 4 ;
int months = - 11 ;
int days = 0 ;
Period p1 = Period.of(year, months, days);
int daystoAdd = 8 ;
addDays(p1, daystoAdd);
}
}
|
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#plusDays-long-
Last Updated :
27 Nov, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...