TimeUnit valueOf() method in Java with Examples
The valueOf() method of TimeUnit Class returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
Syntax:
public static TimeUnit valueOf(String name)
Parameters: This method accepts a mandatory parameter name which is the name of the enum constant to be returned.
Return Value: This method returns the enum constant with the specified name
Exception: This method throws following exceptions:
- IllegalArgumentException– if this enum type has no constant with the specified name
- NullPointerException– if the argument is null
Below program illustrate the implementation of TimeUnit valueOf() method:
Program 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
Program 2: To demonstrate 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
Program 3: To demonstrate 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
Recommended Posts:
- TimeUnit toMinutes() method in Java with Examples
- TimeUnit toSeconds() method in Java with Examples
- TimeUnit toHours() method in Java with Examples
- TimeUnit toMicros() method in Java with Examples
- TimeUnit toNanos() method in Java with Examples
- TimeUnit convert() method in Java with Examples
- TimeUnit sleep() method in Java with Examples
- TimeUnit toDays() method in Java with Examples
- TimeUnit values() method in Java with Examples
- TimeUnit toMillis() method in Java with Examples
- DayOfWeek valueOf() method in Java with Examples
- Java.lang.Long.valueOf() method with examples
- Locale.Category valueOf() method in Java with Examples
- Locale.FilteringMode valueOf() method in Java with Examples
- Character.valueOf() in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

