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


Java DayOfWeek plus()用法及代码示例


java.time.DayOfWeek的plus()方法是Java中的内置函数,该函数使用一个长整数作为参数,并在向前或向后按传递的参数指定的几天前进后返回DayOfWeek的实例。计算从星期日到星期一在周末结束时进行。指定的时间段可以是正数或负数。

方法声明:

public DayOfWeek plus(long days)

用法:


DayOfWeek dayOfWeekObject = dayOfWeekObject.plus(long days)

参数:此方法以天为参数,其中:

  • days–是前进或后退的天数。
  • dayOfWeekObject–是DayOfWeek对象的实例。

返回值:在向前或向后前进几天后,该函数将返回DayOfWeek的实例。

以下示例程序旨在说明上述方法:
示例1:

// Java Program Demonstrate plus() 
// method of DayOfWeek 
import java.time.DayOfWeek; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Getting an instance of DayOfWeek from int value 
        DayOfWeek dayOfWeek = DayOfWeek.of(2); 
  
        // Printing the day of the week 
        // and its Int value 
        System.out.println("Day of the Week : "
                           + dayOfWeek.name() 
                           + " - "
                           + dayOfWeek.getValue()); 
  
        // Number of days to advance 
        long adv = 10; 
  
        // Advancing the day 
        dayOfWeek = dayOfWeek.plus(adv); 
  
        // Printing the day of the week and its 
        // Int value after adv days 
        System.out.println("Day of the Week after "
                           + adv + " days: "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue()); 
    } 
}
输出:
Day of the Week : TUESDAY - 2
Day of the Week after 10 days: FRIDAY - 5

示例2:

// Java Program Demonstrate plus() 
// method of DayOfWeek 
import java.time.DayOfWeek; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Getting an instance of DayOfWeek 
        // from int value 
        DayOfWeek dayOfWeek = DayOfWeek.of(7); 
  
        // Printing the day of the week 
        // and its Int value 
        System.out.println("Day of the Week : "
                           + dayOfWeek.name() 
                           + " - "
                           + dayOfWeek.getValue()); 
  
        // Number of days to advance 
        long adv = -3; 
  
        // Advancing the day 
        dayOfWeek = dayOfWeek.plus(adv); 
  
        // Printing the day of the week and its 
        // Int value after adv days 
        System.out.println("Day of the Week after "
                           + adv + " days: "
                           + dayOfWeek.name() 
                           + " - "
                           + dayOfWeek.getValue()); 
    } 
}
输出:
Day of the Week : SUNDAY - 7
Day of the Week after -3 days: THURSDAY - 4

参考: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#plus-long-



相关用法


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