ChronoUnit values() method in Java with Examples

The values() method of ChronoUnit enum is used to an array containing the units of this ChronoUnit, in the order, they are declared.
Syntax:
public static ChronoUnit[] values()
Parameters: NA
Return value: This method returns an array containing the constants of this enum type, in the order, they are declared. Below programs illustrate the ChronoUnit.values() method:
Program 1:
Java
// Java program to demonstrate// ChronoUnit.values() methodimport java.time.temporal.ChronoUnit;public class GFG {    // Main driver method    public static void main(String[] args)    {        // get ChronoUnit        ChronoUnit chronounit            = ChronoUnit.valueOf(" FOREVER & quot;);        // apply values()        ChronoUnit[] array = chronounit.values();        // print        for (int i = 0; i & lt; array.length; i++)            System.out.println(array[i]);    }} | 
Output:
Nanos Micros Millis Seconds Minutes Hours HalfDays Days Weeks Months Years Decades Centuries Millennia Eras Forever
Program 2:
Java
// Java program to demonstrate// ChronoUnit.values() methodimport java.time.temporal.ChronoUnit;public class GFG {    public static void main(String[] args)    {        // get ChronoUnit        ChronoUnit chronounit            = ChronoUnit.valueOf("CENTURIES");        // apply values()        ChronoUnit[] array            = chronounit.values();        // print        System.out.println("ChronoUnit length:"                           + array.length);    }} | 
Output:
ChronoUnit length:16
Program 3:
Java
import java.io.*;import java.time.temporal.ChronoUnit;// classpublic class GFG {       //  Main driver method  public static void main(String[] args)    {               ChronoUnit[] units = ChronoUnit.values();        ChronoUnit smallest = units[0];        ChronoUnit largest = units[0];           // Iterating using enhanced for each loop    for (ChronoUnit unit : units) {            if (unit.compareTo(smallest) < 0) {                smallest = unit;            }            if (unit.compareTo(largest) > 0) {                largest = unit;            }        }         // Print statements        System.out.println("Smallest unit: " + smallest);        System.out.println("Largest unit: " + largest);    }} | 
Output:
Smallest unit: Nanos Largest unit: Forever
References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/ChronoUnit.html#values()
				
					


