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


Java DayOfWeek valueOf()用法及代码示例


java.time.DayOfWeek的valueOf()方法是Java中的内置函数,该函数接收字符串,并返回与该字符串对应的DayOfWeek实例。该字符串必须与用于声明此类型的day-of-week常数的标识符完全匹配。不允许使用多余的空格字符。

方法声明:

 public static DayOfWeek valueOf(String name)

用法:


 DayOfWeek dayOfWeekObject = DayOfWeek.valueOf(String name)

参数:此方法将名称作为参数,其中:

  • name–是字符串,它是day-of-week的名称。
  • dayOfWeekObject–是DayOfWeek的实例。

返回值:该函数返回与名称字符串相对应的DayOfWeek实例

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

// Java Program Demonstrate valueOf() 
// method of DayOfWeek 
  
import java.time.*; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Initializing a DayOfWeek instance 
        DayOfWeek dayOfWeek 
            = DayOfWeek.valueOf("MONDAY"); 
  
        // Printing the day-of-week 
        System.out.println(dayOfWeek.name()); 
    } 
}
输出:
MONDAY

示例2:

// Java Program Demonstrate valueOf() 
// method of DayOfWeek 
  
import java.time.*; 
  
class DayOfWeekExample { 
    public static void main(String[] args) 
    { 
        // Initializing a DayOfWeek instance 
        DayOfWeek dayOfWeek 
            = DayOfWeek.valueOf("SATURDAY"); 
  
        // Printing the day-of-week 
        System.out.println(dayOfWeek.name()); 
    } 
}
输出:
SATURDAY

参考: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#valueOf-java.lang.String-



相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 DayOfWeek valueOf() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。