从Yearyearth对象减去指定的TemporalAmount数量后,使用YearMonth类的minus(TemporalAmount)方法返回Yearyear的副本。如果无法从YearMonth中减去指定的单位,则会引发异常。传递的TemporalAmount是根据Period对象创建的。此实例是不可变的,不受此方法调用的影响。
用法:
public YearMonth minus(TemporalAmount amountToSubtract)
参数:此方法仅接受一个参数TemporalAmount,该参数表示从YearMonth对象减去的数量。
返回值:此方法基于此YearMonth返回YearMonth,减去指定的数量。
异常:此方法引发以下异常:
- DateTimeException-如果无法进行减法,则抛出此异常。
- ArithmeticException-如果发生数字溢出,则抛出此异常。
以下示例程序旨在说明减号(TemporalAmount amountToSubtract)方法:
示例1:
// Java program to demonstrate
// YearMonth.minus(TemporalAmount amountToSubtract) 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
// minus() method
YearMonth value
= thisYearMonth.minus(
Period.ofYears(22));
// print result
System.out.println("Value after Subtraction: "
+ value);
}
}
输出:
YearMonth :2019-11 Value after Subtraction: 1997-11
示例2:
// Java program to demonstrate
// YearMonth.minus(TemporalAmount amountToSubtract) 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
// minus() method
YearMonth value
= thisYearMonth.minus(
Period.ofMonths(20));
// print result
System.out.println("Value after Subtraction: "
+ value);
}
}
输出:
YearMonth :2022-11 Value after Subtraction: 2021-03
相关用法
- Java YearMonth until()用法及代码示例
- Java YearMonth with()用法及代码示例
- Java YearMonth withYear()用法及代码示例
- Java YearMonth isAfter()用法及代码示例
- Java YearMonth plus(TemporalAmount)用法及代码示例
- 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 isSupported(TemporalField)用法及代码示例
- Java YearMonth isSupported(TemporalUnit)用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 YearMonth minus(TemporalAmount) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。