LocalDate类plusDays()方法 (LocalDate Class plusDays() method)
plusDays() method is available in java.time package.
plusDays()方法在java.time包中可用。
plusDays() method is used to add the given duration in days to this LocalDate and return the LocalDate.
plusDays()方法用于将以天为单位的给定持续时间添加到此LocalDate并返回LocalDate。
plusDays() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
plusDays()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名访问该方法,则会收到错误消息。
plusDays() method may throw an exception at the time of performing addition.
plusDays()方法在执行加法时可能会引发异常。
DateTimeException: This exception may throw when the calculated results exceed the length of this LocalDate.
DateTimeException :当计算结果超过此LocalDate的长度时,可能引发此异常。
Syntax:
句法:
public LocalDate plusDays(long day_val);
Parameter(s):
参数:
long day_val – represents the day value to add to this LocalDate.
long day_val –表示要添加到此LocalDate的日期值。
Return value:
返回值:
The return type of this method is LocalDate, it returns the LocalDate that holds the value added the given days to this LocalDate.
此方法的返回类型为LocalDate ,它返回LocalDate,该LocalDate包含将给定天数添加到此LocalDate的值。
Example:
例:
// Java program to demonstrate the example
// of plusDays(long day_val) method of LocalDate
import java.time.*;
public class PlusDaysOfLocalDate {
public static void main(String args[]) {
long days = 10;
// Instantiates two LocalDate
LocalDate l_da1 = LocalDate.parse("2007-04-04");
LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);
// Display l_da1,l_da2 and days
System.out.println("LocalDate l_da1,l_da2 : ");
System.out.println("l_da1: " + l_da1);
System.out.println("l_da2: " + l_da2);
System.out.println("days to add: " + days);
System.out.println();
// Here, this method adds the given
// days to this date l_da1
// i.e. here we are adding 10
// days to the date l_da1
LocalDate plus_days = l_da1.plusDays(days);
// Display plus_days
System.out.println("l_da1.plusDays(days): " + plus_days);
// Here, this method adds the given
// days to this date l_da2
// i.e. here we are adding 10
// days to the date l_da2
plus_days = l_da2.plusDays(days);
// Display plus_days
System.out.println("l_da2.plusDays(days): " + plus_days);
}
}
Output
输出量
LocalDate l_da1,l_da2 :
l_da1: 2007-04-04
l_da2: 2008-02-06
days to add: 10
l_da1.plusDays(days): 2007-04-14
l_da2.plusDays(days): 2008-02-16
翻译自: https://www.includehelp.com/java/localdate-plusdays-method-with-example.aspx