📌  相关文章
📜  Java中的 YearMonth plus(TemporalAmount) 方法及示例

📅  最后修改于: 2022-05-13 01:54:22.558000             🧑  作者: Mango

Java中的 YearMonth plus(TemporalAmount) 方法及示例

YearMonth类的plus(TemporalAmount)方法用于将指定数量的 TemporalAmount 添加到此 YearMonth 对象后返回此 YearMonth 的副本。如果无法将指定的单位添加到 YearMonth,则会引发异常。传递的 TemporalAmount 是从 Period 对象创建的。此实例是不可变的,不受此方法调用的影响。

句法:

public YearMonth plus(TemporalAmount amountToadd)

参数:此方法只接受一个参数TemporalAmount ,它表示要添加到 YearMonth 对象的数量。

返回值:此方法基于此 YearMonth 返回一个 YearMonth,并添加指定的金额。

异常:此方法抛出以下异常:

  • DateTimeException –如果无法添加,则会引发此异常。
  • ArithmeticException –如果发生数字溢出,则会引发此异常。

下面的程序说明了 plus(TemporalAmount amountToadd) 方法:

方案一:

Java
// Java program to demonstrate
// YearMonth.plus(TemporalAmount amountToadd) method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2019, 11);
 
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
 
        // create temporalAmount object from period class
        // passing temporalAmount to
        // plus() method
        YearMonth value
            = thisYearMonth.plus(
                Period.ofYears(120));
 
        // print result
        System.out.println("Value after addition: "
                           + value);
    }
}


Java
// Java program to demonstrate
// YearMonth.plus(TemporalAmount amountToadd) method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2022, 11);
 
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
 
        // create temporalAmount object from period class
        // passing temporalAmount to
        // plus() method
        YearMonth value
            = thisYearMonth.plus(
                Period.ofMonths(200));
 
        // print result
        System.out.println("Value after addition: "
                           + value);
    }
}


输出:
YearMonth :2019-11
Value after addition: 2139-11

方案二:

Java

// Java program to demonstrate
// YearMonth.plus(TemporalAmount amountToadd) method
 
import java.time.*;
import java.time.temporal.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a YearMonth object
        YearMonth thisYearMonth = YearMonth.of(2022, 11);
 
        // print instance
        System.out.println("YearMonth :"
                           + thisYearMonth);
 
        // create temporalAmount object from period class
        // passing temporalAmount to
        // plus() method
        YearMonth value
            = thisYearMonth.plus(
                Period.ofMonths(200));
 
        // print result
        System.out.println("Value after addition: "
                           + value);
    }
}
输出:
YearMonth :2022-11
Value after addition: 2039-07

参考:
https://docs.oracle.com/javase/10/docs/api/java Java Java)