ChronoUnit valueOf() method in Java with Examples

Last Updated : 29 Jan, 2020
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
// 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
// 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);
    }
}
Comment