Java 中的 Period plusDays()方法,示例

原文:https://www . geesforgeks . org/period-plus days-method-in-Java-with-examples/

Java 中 Period 类的 plusDays()方法用于将天数添加到此期间。此方法仅在 DAYS 上运行,不影响其他两个 YEAR,MONTH。

语法:

public Period plusDays(long daysToAdd)

参数:此方法接受单个参数天数添加,即从周期中添加的天数。

返回值:根据输入中提供的期间,加上指定的天数,返回一个期间。它不能为空。

异常:如果出现数值溢出,则抛出算术异常

下面的程序说明了上述方法:

程序 1 :

// Java code to show the function plusDays()
// to subtract the number of days from given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class PeriodClass {

    // Function to subtract two given periods
    static void addDays(Period p1, int daystoAdd)
    {

        System.out.println(p1.plusDays(daystoAdd));
    }

    // Driver Code
    public static void main(String[] args)
    {
        // Defining first period
        int year = 4;
        int months = 11;
        int days = 10;
        Period p1 = Period.of(year, months, days);

        int daystoAdd = 8;

        addDays(p1, daystoAdd);
    }
}

输出:

P4Y11M18D

程序二:期间可以为负数。

// Java code to show the function plusDays()
// to subtract the number of days from given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class PeriodClass {

    // Function to subtract two given periods
    static void addDays(Period p1, int daystoAdd)
    {

        System.out.println(p1.plusDays(daystoAdd));
    }

    // Driver Code
    public static void main(String[] args)
    {

        // Defining first period
        int year = -4;
        int months = -11;
        int days = 0;
        Period p1 = Period.of(year, months, days);

        int daystoAdd = 8;

        addDays(p1, daystoAdd);
    }
}

输出:

P-4Y-11M8D

参考:https://docs . Oracle . com/javase/8/docs/API/Java/time/period . html # plusDays-long-