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
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
ChronoUnit chronounit
= ChronoUnit.valueOf(" FOREVER & quot;);
ChronoUnit[] array = chronounit.values();
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
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
ChronoUnit chronounit
= ChronoUnit.valueOf("CENTURIES");
ChronoUnit[] array
= chronounit.values();
System.out.println("ChronoUnit length:"
+ array.length);
}
}
|
Output:
ChronoUnit length:16
Program 3:
Java
import java.io.*;
import java.time.temporal.ChronoUnit;
public class GFG {
public static void main(String[] args)
{
ChronoUnit[] units = ChronoUnit.values();
ChronoUnit smallest = units[ 0 ];
ChronoUnit largest = units[ 0 ];
for (ChronoUnit unit : units) {
if (unit.compareTo(smallest) < 0 ) {
smallest = unit;
}
if (unit.compareTo(largest) > 0 ) {
largest = unit;
}
}
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()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!