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


Java TimeUnit valueOf()用法及代碼示例


TimeUnit類的valueOf()方法返回具有指定名稱的此類型的枚舉常量。該字符串必須與用於聲明此類型的枚舉常量的標識符完全匹配。 (不允許使用多餘的空格字符。)

用法:

public static TimeUnit valueOf(String name)

參數:此方法接受必需的參數名稱,該名稱是要返回的枚舉常量的名稱。


返回值:此方法返回具有指定名稱的枚舉常量

異常:此方法引發以下異常:

  • IllegalArgumentException–如果此枚舉類型沒有帶有指定名稱的常量
  • NullPointerException –如果參數為null

以下示例程序旨在說明TimeUnit valueOf()方法的實現:

示例1:

// Java program to demonstrate 
// valueOf() method of TimeUnit Class 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
  
        // Create an object of TimeUnit class 
        // using valueOf() method 
        // Below statement is equivalent to 
        // TimeUnit Days = TimeUnit.DAYS; 
        TimeUnit Days = TimeUnit.valueOf("DAYS"); 
  
        // Print the Enum of TimeUnit Object 
        System.out.println("TimeUnit object "
                           + "is of type: "
                           + Days); 
  
        // Convert current object to Hours 
        System.out.println("1 Day = "
                           + Days.toHours(1) 
                           + " Hours"); 
    } 
}
輸出:
TimeUnit object is of type: DAYS
1 Day = 24 Hours

示例2:演示NullPointerException

// Java program to demonstrate 
// valueOf() method of TimeUnit Class 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
  
        try { 
  
            System.out.println("Trying to create "
                               + "TimeUnit object "
                               + "using null Enum type"); 
  
            // Create an object of TimeUnit class 
            // using valueOf() method 
            // by passing null as parameter 
            TimeUnit Days = TimeUnit.valueOf(null); 
        } 
  
        catch (NullPointerException e) { 
            System.out.println("\nException thrown: " + e); 
        } 
    } 
}
輸出:
Trying to create TimeUnit object using null Enum type

Exception thrown: java.lang.NullPointerException: Name is null

示例3:演示IllegalArgumentException

// Java program to demonstrate 
// valueOf() method of TimeUnit Class 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String args[]) 
    { 
  
        try { 
  
            System.out.println("Trying to create "
                               + "TimeUnit object "
                               + "using ABCD Enum type"); 
  
            // Create an object of TimeUnit class 
            // using valueOf() method 
            // by passing ABCD as parameter 
            TimeUnit Days = TimeUnit.valueOf("ABCD"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("\nException thrown: " + e); 
        } 
    } 
}
輸出:
Trying to create TimeUnit object using ABCD Enum type

Exception thrown: java.lang.IllegalArgumentException: No enum constant java.util.concurrent.TimeUnit.ABCD


相關用法


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