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


Java DayOfWeek minus()用法及代碼示例


java.time.DayOfWeek的minus()方法是Java中的一個內置函數,該函數使用一個長整數作為參數,並在按傳遞的參數指定的前進或後退幾天後返回DayOfWeek的實例。計算從星期一到星期日的一周結束時進行。指定的時間段可以是正數或負數。

方法聲明:

public DayOfWeek minus(long days)

用法:


DayOfWeek dayOfWeekObject = dayOfWeekObject.minus(long days)

參數:此方法以天為參數,其中:

  • days–是前進或後退的天數。
  • dayOfWeekObject–是DayOfWeek對象的實例。

返回值:向後或向前前進幾天後,該函數將返回DayOfWeek的實例。

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

// Java Program Demonstrate minus() 
// 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.minus(adv); 
  
        // Printing the day of the week and its 
        // Int value before adv days 
        System.out.println("Day of the Week before "
                           + adv + " days: "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue()); 
    } 
}
輸出:
Day of the Week : TUESDAY - 2
Day of the Week before 10 days: SATURDAY - 6

示例2:

// Java Program Demonstrate minus() 
// 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.minus(adv); 
  
        // Printing the day of the week and its 
        // Int value before adv days 
        System.out.println("Day of the Week before "
                           + adv + " days: "
                           + dayOfWeek.name() 
                           + " - "
                           + dayOfWeek.getValue()); 
    } 
}
輸出:
Day of the Week : SUNDAY - 7
Day of the Week before -3 days: WEDNESDAY - 3

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



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 DayOfWeek minus() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。