Java中YearMonth类的plusYears()方法用于返回此YearMonth的副本,其中添加了指定的年数。
用法:
public YearMonth plusYears(long yearsToAdd)
参数:此方法接受yearsToAdd作为参数,表示要添加到当前YearMonth对象的年份。可能是负面的。
返回值:它基于此year-month返回添加了年份的YearMonth。
异常:如果结果超出支持的范围,则此方法将引发DateTimeException。
以下示例程序旨在说明Java中YearMonth的plusYears()方法:
程序1:
// Java program to demonstrate
// YearMonth.plusYears() 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");
// Apply plusYears() method
// of YearMonth class
YearMonth result
= yearmonth.plusYears(5);
// It will add 5 years into May 2020
// So it will be May 2025
// print results
System.out.println(
"Modified YearMonth:"
+ result);
}
}
输出:
Modified YearMonth:2025-05
程序2:
// Java program to demonstrate
// YearMonth.plusYears() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// Create YearMonth object
YearMonth yearmonth
= YearMonth.of(2019, 10);
// It is October 2019
// apply plusYears() method
// of YearMonth class
YearMonth result
= yearmonth.plusYears(10);
// YearMonth will become October 2029
// print only modified year
System.out.println(
"Modified Year:"
+ result.get(ChronoField.YEAR));
}
}
输出:
Modified Year:2029
参考文献: https://docs.oracle.com/javase/10/docs/api/java/time/YearMonth.html#plusYears(long)
相关用法
- Java LocalDate plusYears()用法及代码示例
- Java LocalDateTime plusYears()用法及代码示例
- Java OffsetDateTime plusYears()用法及代码示例
- Java Year plusYears()用法及代码示例
- Java ZonedDateTime plusYears()用法及代码示例
- Java Period plusYears()用法及代码示例
- Java YearMonth until()用法及代码示例
- Java YearMonth now()用法及代码示例
- Java YearMonth with()用法及代码示例
- Java YearMonth get()用法及代码示例
- Java YearMonth withYear()用法及代码示例
- Java YearMonth getLong()用法及代码示例
- Java YearMonth query()用法及代码示例
- Java YearMonth plusMonths()用法及代码示例
- Java YearMonth lengthOfYear()用法及代码示例
注:本文由纯净天空筛选整理自pp_pankaj大神的英文原创作品 YearMonth plusYears() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。