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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}

chevron_right


Output:

TimeUnit object is of type: DAYS
1 Day = 24 Hours

Program 2: To demonstrate NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Trying to create TimeUnit object using null Enum type

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

Program 3: To demonstrate IllegalArgumentException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

Trying to create TimeUnit object using ABCD Enum type

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


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

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.