The Wayback Machine - https://web.archive.org/web/20240629004213/https://www.geeksforgeeks.org/chronounit-values-method-in-java-with-examples/
Open In App

ChronoUnit values() method in Java with Examples

Last Updated : 27 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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]);
    }
}


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() 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

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()



Similar Reads

ChronoUnit isTimeBased() method in Java with Examples
The isTimeBased() method of ChronoUnit enum is used to check if this ChronoUnit is a time unit. All ChronoUnits from nanos to half-days are time-based. Syntax: public boolean isTimeBased() Parameters: This method accepts nothing. Return value: This method returns true if a time unit, false if a data unit. Below programs illustrate the ChronoUnit.is
1 min read
ChronoUnit getDuration() method in Java with Examples
The getDuration() method of ChronoUnit enum is used to return the estimated duration of this ChronoUnit in the ISO calendar system.Days vary due to daylight saving time, while months have different lengths. Syntax: public Duration getDuration() Parameters: This method accepts nothing. Return value: This method returns the estimated duration of this
1 min read
ChronoUnit isDateBased() method in Java with Examples
The isDateBased() method of ChronoUnit enum is used to check if this ChronoUnit is a date unit. All ChronoUnits from days to eras are date-based. Syntax: public boolean isDateBased() Parameters: This method accepts nothing. Return value: This method returns true if a date unit, false if a time unit. Below programs illustrate the ChronoUnit.isDateBa
1 min read
ChronoUnit valueOf() method in Java with Examples
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 m
1 min read
ChronoUnit isDurationEstimated() method in Java with Examples
The isDurationEstimated() method of ChronoUnit enum is used to check if the duration of the ChronoUnit is an estimate. All the time units in this ChronoUnit are considered to be accurate, while all date units in this ChronoUnit are considered to be estimated. Syntax: public boolean isDurationEstimated() Parameters: This method accepts nothing. Retu
1 min read
Map Values() Method in Java With Examples
Map Values() method returns the Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. Syntax: map.values() Parameter: This method does not take any parameter. Return value: It returns a collection view of all the values present in the map. Exa
2 min read
TreeMap values() Method in Java with Examples
In Java, the values() method of the TreeMap class is present inside java.util package which is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the TreeMap. --&gt; java.util package --&gt; TreeMap class --&gt; values() Method Syntax: Tree_Map.values() Return Type: A collection view co
2 min read
Provider values() method in Java with Examples
The values() method of java.security.Provider class is used to return an unmodifiable Collection view of the property values contained in this provider.Syntax: public Collection&lt;Object&gt; values() Return Value: This method returns a collection view of the values contained in this map.Below are the examples to illustrate the values() method:Exam
3 min read
TimeUnit values() method in Java with Examples
The values() method of TimeUnit Class returns an array containing the constants of this enum type, in the order they are declared. In short, this method is used to get the Enum Constants of TimeUnit Class. Syntax: public static TimeUnit[] values() Return Value: This method returns an array containing the constants of this enum type, in the order th
1 min read
AbstractMap values() Method in Java with Examples
The AbstractMap.values() method of AbstractMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the AbstractMap. Syntax: AbstractMap.values() Parameters: The method does not accept any parameters. Return Value: The method is used to return a collection view containing
2 min read
three90RightbarBannerImg