📜  Java中的 DayOfWeek of() 方法及示例

📅  最后修改于: 2022-05-13 01:55:48.027000             🧑  作者: Mango

Java中的 DayOfWeek of() 方法及示例

Java .time.DayOfWeekof()方法是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 对象的一个实例。

下面的程序说明了上述方法:
方案一:

// 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

方案二:

// 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: Java/time/DayOfWeek.html#of-int-