Java中LocalDate類的minusDays()方法用於從此LocalDate中減去指定天數並返回LocalDate的副本,例如2018-01-01減去1天將得出2018-12-31。此實例是不可變的,不受此方法調用的影響。
用法:
public LocalDate minusDays(long daysToSubtract)
參數:此方法接受單個參數daysToSubtract,該參數表示要減去的天數,可以為負數。
返回值:此方法基於此日期(減去減去的天)返回LocalDate,而不是null。
異常:如果結果超出支持的日期範圍,則此方法將引發DateTimeException。
以下示例程序旨在說明minusDays()方法:
示例1:
// Java program to demonstrate
// LocalDate.minusDays() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate date
= LocalDate.parse("2018-11-13");
// print instance
System.out.println("LocalDate before"
+ " subtracting days: " + date);
// subtract 17 days
LocalDate returnvalue
= date.minusDays(17);
// print result
System.out.println("LocalDate after "
+ " subtracting days: " + returnvalue);
}
}
輸出:
LocalDate before subtracting days: 2018-11-13 LocalDate after subtracting days: 2018-10-27
示例2:
// Java program to demonstrate
// LocalDate.minusDays() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create a LocalDate object
LocalDate date
= LocalDate.parse("2018-12-24");
// print instance
System.out.println("LocalDate before"
+ " subtracting days: " + date);
// subtract -15 days
LocalDate returnvalue
= date.minusDays(-15);
// print result
System.out.println("LocalDate after "
+ " subtracting days: " + returnvalue);
}
}
輸出:
LocalDate before subtracting days: 2018-12-24 LocalDate after subtracting days: 2019-01-08
參考:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDate.html#minusDays(long)
相關用法
- Java Period minusDays()用法及代碼示例
- Java OffsetDateTime minusDays()用法及代碼示例
- Java ZonedDateTime minusDays()用法及代碼示例
- Java Duration minusDays(long)用法及代碼示例
- Java LocalDate now()用法及代碼示例
- Java LocalDate with()用法及代碼示例
- Java LocalDate get()用法及代碼示例
- Java LocalDate plus()用法及代碼示例
- Java LocalDate now()用法及代碼示例
- Java LocalDate minusMonths()用法及代碼示例
- Java LocalDate plusYears()用法及代碼示例
- Java LocalDate minusWeeks()用法及代碼示例
- Java LocalDate parse()用法及代碼示例
- Java LocalDate plusMonths()用法及代碼示例
- Java LocalDate plusWeeks()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 LocalDate minusDays() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。