Saturday, November 22, 2025
HomeLanguagesJavaChronoUnit valueOf() method in Java with Examples

ChronoUnit valueOf() method in Java with Examples

The valueOf() method of ChronoUnit enum is used to return the enum unit of this type with the specified name.

Syntax:

public static ChronoUnit valueOf(String name)

Parameters: This method accepts 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 the following exception:

  • IllegalArgumentException: if this enum type has no constant with the specified name.
  • NullPointerException: if the argument is null.

Below programs illustrate the ChronoUnit.valueOf() method:
Program 1:




// Java program to demonstrate
// ChronoUnit.valueOf() method
  
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get ChronoUnit
        ChronoUnit chronoUnit
            = ChronoUnit.valueOf("MILLENNIA");
  
        // print
        System.out.println("ChronoUnit: "
                           + chronoUnit);
    }
}


Output:

ChronoUnit: Millennia

Program 2:




// Java program to demonstrate
// ChronoUnit.valueOf() method
  
import java.time.temporal.ChronoUnit;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get ChronoUnit
        ChronoUnit chronoUnit
            = ChronoUnit.valueOf("CENTURIES");
  
        // print
        System.out.println("ChronoUnit: "
                           + chronoUnit);
    }
}


Output:

ChronoUnit: Centuries

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ChronoUnit.html#valueOf(java.lang.String)

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS