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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。