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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。