当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LocalDate minusDays()用法及代码示例


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)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 LocalDate minusDays() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。