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