Java中YearMonth类的minusMonths()方法用于返回该YearMonth的副本,其中减去指定的月数。
用法:
public YearMonth minusMonths( long monthsToSubtract)
参数:此方法接受monthsToSubtract作为参数,代表要减去的月份。可能是负面的。
返回值:它基于此year-month减去后的月份返回YearMonth。
异常:如果结果超出支持的范围,则此方法将引发DateTimeException。
以下示例程序旨在说明Java中YearMonth的minusMonths()方法:
程序1:
// Java program to demonstrate
// YearMonth.minusMonths() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create YearMonth object
YearMonth yearmonth
= YearMonth.parse("2020-05");
// It is May 2020
// apply minusMonths() method
// of YearMonth class
YearMonth result
= yearmonth.minusMonths(2);
// It subtracts 2 months from May 2020
// So it will be March 2020
// print year and month both
System.out.println("Modified YearMonth:"
+ result);
}
}
输出:
Modified YearMonth:2020-03
程序2:
// Java program to demonstrate
// YearMonth.minusMonths() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create YearMonth object
YearMonth yearmonth
= YearMonth.parse("2019-10");
// apply minusMonths() method
// of YearMonth class
YearMonth result
= yearmonth.minusMonths(10);
// Subtrating 10 months will turn it into
// December of 2018 (previous year)
// print year and month both
System.out.println(
"Modified YearMonth:"
+ result);
}
}
输出:
Modified YearMonth:2018-12
参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#minusMonths(long)
相关用法
- Java OffsetDateTime minusMonths()用法及代码示例
- Java ZonedDateTime minusMonths()用法及代码示例
- Java Period minusMonths()用法及代码示例
- Java LocalDate minusMonths()用法及代码示例
- Java YearMonth until()用法及代码示例
- Java YearMonth now()用法及代码示例
- Java YearMonth get()用法及代码示例
- Java YearMonth with()用法及代码示例
- Java YearMonth isValidDay()用法及代码示例
- Java YearMonth isAfter()用法及代码示例
- Java YearMonth range()用法及代码示例
- Java YearMonth isBefore()用法及代码示例
- Java YearMonth plus(TemporalAmount)用法及代码示例
- Java YearMonth isLeapYear()用法及代码示例
- Java YearMonth now(clock)用法及代码示例
注:本文由纯净天空筛选整理自pp_pankaj大神的英文原创作品 YearMonth minusMonths() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。