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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。