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


Java Period withDays()用法及代码示例


时期类的withDays()方法用于获取具有指定天数的时期。该天数作为参数传递为整数值。

用法:

public Period withDays(int numberOfDays)

参数:此方法接受参数numberOfDays,它是此期间要更改的天数。


返回值:此函数返回以传递的天数为参数的Period。

下面是Period.withDays()方法的实现:

示例1:

// Java code to demonstrate withDays() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the String to be withDaysd 
        String period = "P1Y2M21D"; 
  
        // Parse the String into Period 
        Period p = Period.parse(period); 
  
        System.out.println("Period: " + p); 
  
        // Get the number of days 
        int numberOfDays = 5; 
  
        // Change the numberOfDays of this Period 
        // using withDays() method 
        System.out.println("New Period: "
                           + p.withDays(numberOfDays)); 
    } 
}
输出:
Period: P1Y2M21D
New Period: P1Y2M5D

示例2:

// Java code to demonstrate withDays() method 
  
import java.time.Period; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the String to be withDaysd 
        String period = "-P1Y2M21D"; 
  
        // Parse the String into Period 
        Period p = Period.parse(period); 
  
        System.out.println("Period: " + p); 
  
        // Get the number of days 
        int numberOfDays = -5; 
  
        // Change the numberOfDays of this Period 
        // using withDays() method 
        System.out.println("New Period: "
                           + p.withDays(numberOfDays)); 
    } 
}
输出:
Period: P-1Y-2M-21D
New Period: P-1Y-2M-5D

参考: https://docs.oracle.com/javase/9/docs/api/java/time/Period.html#withDays-int-



相关用法


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