Java 中的 Period plusMonths()方法,带示例

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

Java 中 Period 类的 plusMonths()方法用于将指定的月份添加到当前期间。该功能仅在月运行,不影响年和日。

语法:

public Period plusMonths(long monthsToAdd)

参数:此方法接受单个参数月至日,即要添加到期间的月数。

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

异常:如果出现数值溢出,这个方法抛出算术异常

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

程序 1:

// Java code to show the function plusMonths()
// to add the number of months to given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class PeriodClass {

    // Function to add months to given periods
    static void addMonths(Period p1, int monthstoAdd)
    {

        System.out.println(p1.plusMonths(monthstoAdd));
    }

    // 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 monthstoAdd = -8;

        addMonths(p1, monthstoAdd);
    }
}

输出:

P-4Y-19M

程序二:待加月份可以为负数。

// Java code to show the function plusMonths()
// to add the number of months from given periods
import java.time.Period;
import java.time.temporal.ChronoUnit;

public class PeriodClass {

    // Function to add months to given period
    static void addMonths(Period p1, int monthstoAdd)
    {

        System.out.println(p1.plusMonths(monthstoAdd));
    }

    // 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 monthstoAdd = 8;

        addMonths(p1, monthstoAdd);
    }
}

输出:

P4Y19M10D

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