當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。