在將指定數量的TemporalUnit添加到此Year-month對象之後,YearMonth類的plus(long,unit)方法用於返回此Year-month的副本。如果無法將指定的單位添加到Year-month,則會引發異常。此實例是不可變的,不受此方法調用的影響。
用法:
public YearMonth plus(long amountToadd, TemporalUnit unit)
參數:此方法接受兩個參數:
- amountToadd:此參數代表要添加到結果中的單位數量。
- unit此參數表示要添加的量的單位。
返回值:此方法基於此Year-month返回添加了指定量的Year-month。
異常:此方法引發以下異常:
- DateTimeException-如果無法進行加法運算,則拋出此異常。
- UnsupportedTemporalTypeException-如果不支持該單元,則拋出此異常。
- ArithmeticException-如果發生數字溢出,則拋出此異常。
以下示例程序旨在說明plus(long,unit)方法:
程序1:
// Java program to demonstrate
// YearMonth.plus(long, unit) 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(2017, 8);
// print instance
System.out.println("YearMonth:"
+ thisYearMonth);
// apply plus(long, unit) method
// adding 15 Years
YearMonth value
= thisYearMonth.plus(15, ChronoUnit.YEARS);
// print result
System.out.println("After addition YearMonth:"
+ value);
}
}
輸出:
YearMonth:2017-08 After addition YearMonth:2032-08
程序2:
// Java program to demonstrate
// YearMonth.plus(long, unit) 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, 12);
// print instance
System.out.println("YearMonth:"
+ thisYearMonth);
// apply plus(long, unit) method
// adding 30 Months
YearMonth value
= thisYearMonth.plus(30, ChronoUnit.MONTHS);
// print result
System.out.println("After addition YearMonth:"
+ value);
}
}
輸出:
YearMonth:2019-12 After addition YearMonth:2022-06
相關用法
- Java YearMonth until()用法及代碼示例
- Java YearMonth with()用法及代碼示例
- Java YearMonth query()用法及代碼示例
- Java YearMonth isAfter()用法及代碼示例
- Java YearMonth lengthOfYear()用法及代碼示例
- Java YearMonth withYear()用法及代碼示例
- Java YearMonth withMonth()用法及代碼示例
- Java YearMonth plus(TemporalAmount)用法及代碼示例
- Java YearMonth isBefore()用法及代碼示例
- Java YearMonth isValidDay()用法及代碼示例
- Java YearMonth range()用法及代碼示例
- Java YearMonth isLeapYear()用法及代碼示例
- Java YearMonth isSupported(TemporalUnit)用法及代碼示例
- Java YearMonth parse(CharSequence)用法及代碼示例
- Java YearMonth isSupported(TemporalField)用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 YearMonth plus(long,unit) method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。