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() method Â
import 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]);     } } |
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() method Â
import 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);     } } |
ChronoUnit length:16
Program 3:
Java
import java.io.*; import java.time.temporal.ChronoUnit; Â
// class public 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()