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


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


java.time.DayOfWeek的of()方法是Java中的內置函數,它從int值返回DayOfWeek的實例。 int值的範圍是1(星期一)至7(星期日)。

方法聲明:

public static DayOfWeek of(int dayOfWeek)

用法:


DayOfWeek dayOfWeekObject = DayOfWeek.of(int dayOfWeek)

參數:此方法將dayOfWeek作為參數,其中:

  • dayOfWeek–是從1(星期一)到7(星期日)的int值。
  • dayOfWeekObject–是DayOfWeek對象的實例。

返回值:該函數返回DayOfWeek對象的實例。

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

// Java Program Demonstrate of() 
// 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(4); 
  
        // Printing the day of the week 
        // and its Int value 
        System.out.println("Day of the Week - "
                           + dayOfWeek.name()); 
        System.out.println("Int Value of "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue()); 
    } 
}
輸出:
Day of the Week - THURSDAY
Int Value of THURSDAY - 4

示例2:

// Java Program Demonstrate of() 
// 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()); 
        System.out.println("Int Value of "
                           + dayOfWeek.name() + " - "
                           + dayOfWeek.getValue()); 
    } 
}
輸出:
Day of the Week - SUNDAY
Int Value of SUNDAY - 7

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



相關用法


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