Skip to content
Related Articles

Related Articles

ChronoUnit values() method in Java with Examples
  • Last Updated : 29 Jan, 2020

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: This method accepts nothing.

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 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("FOREVER");
  
        // apply values()
        ChronoUnit[] array
            = chronounit.values();
  
        // print
        for (int i = 0; i < 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 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);
    }
}
Output:
ChronoUnit length:16

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

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :