在將指定數量的TemporalAmount添加到Year Yearth對象之後,使用YearMonth類的plus(TemporalAmount)方法返回Year Yearth的副本。如果無法將指定的單位添加到YearMonth,則會引發異常。傳遞的TemporalAmount是根據Period對象創建的。此實例是不可變的,不受此方法調用的影響。
用法:
public YearMonth plus(TemporalAmount amountToadd)
參數:此方法僅接受一個參數TemporalAmount,該參數表示要添加到YearMonth對象的數量。
返回值:此方法基於此YearMonth返回YearMonth,並添加指定的數量。
異常:此方法引發以下異常:
- DateTimeException-如果無法進行加法運算,則拋出此異常。
- ArithmeticException-如果發生數字溢出,則拋出此異常。
以下示例程序旨在說明plus(TemporalAmount amountToadd)方法:
示例1:
// 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);
}
}
輸出:
YearMonth :2019-11 Value after addition: 2139-11
示例2:
// 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
相關用法
- Java YearMonth until()用法及代碼示例
- Java YearMonth with()用法及代碼示例
- Java YearMonth withYear()用法及代碼示例
- Java YearMonth isAfter()用法及代碼示例
- Java YearMonth range()用法及代碼示例
- Java YearMonth isBefore()用法及代碼示例
- Java YearMonth query()用法及代碼示例
- Java YearMonth isValidDay()用法及代碼示例
- Java YearMonth lengthOfYear()用法及代碼示例
- Java YearMonth isLeapYear()用法及代碼示例
- Java YearMonth withMonth()用法及代碼示例
- Java YearMonth parse(CharSequence)用法及代碼示例
- Java YearMonth minus(TemporalAmount)用法及代碼示例
- Java YearMonth isSupported(TemporalField)用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 YearMonth plus(TemporalAmount) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。