可以使用minusWeeks()
Java中LocalDate类中的方法获得从中减去周的LocalDate的不可变副本。此方法需要一个参数,即要减去的周数,并返回带有减去的周数的瞬间。
演示此的程序如下所示-
import java.time.*; public class Demo { public static void main(String[] args) { LocalDate ld1 = LocalDate.parse("2019-02-14"); System.out.println("The LocalDate is: " + ld1); LocalDate ld2 = ld1.minusWeeks(2); System.out.println("The LocalDate after subtracting 2 weeks is: " + ld2); } }
输出结果
The LocalDate is: 2019-02-14 The LocalDate after subtracting 2 weeks is: 2019-01-31
现在让我们了解上面的程序。
首先显示LocalDate。然后使用该minusWeeks()
方法获得LocalDate的不变副本,其中减去2周,并将其显示出来。演示这的代码片段如下-
LocalDate ld1 = LocalDate.parse("2019-02-14"); System.out.println("The LocalDate is: " + ld1); LocalDate ld2 = ld1.minusWeeks(2); System.out.println("The LocalDate after subtracting 2 weeks is: " + ld2);