Java Instant Class | range() Method with Example

Instant Class range() method: Here, we are going to learn about the range() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020

Instant Class range() method

  • range() method is available in java.time package.
  • range() method is used to get the valid range of values for the given TemporalField.
  • range() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • range() method may throw an exception at the time of returning field range.
    • DateTimeException: This exception may throw when the given field value couldn't be generated.
    • UnsupportedTemporalTypeException: This exception may throw when the given temporal field is unsupported.

Syntax:

    public ValueRange range(TemporalField t_field);

Parameter(s):

  • TemporalField t_field – represents the field of the returned values range.

Return value:

The return type of this method is ValueRange, it returns range of values for the given field in this Instant.

Example:

// Java program to demonstrate the example 
// of ValueRange range(TemporalField t_field)
// method of Instant

import java.time.*;
import java.time.temporal.*;

public class RangeOfInstant {
    public static void main(String args[]) {
        // Instantiates two Instant
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();

        // Display ins1,ins2 
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);

        System.out.println();

        // Here, this method gets the valid 
        // range of values for the given field  
        // i.e. here the MICRO_OF_SECOND field 
        // range will be returned 
        ValueRange range = (ValueRange) ins1.range(ChronoField.MICRO_OF_SECOND);

        // Display range
        System.out.println("ins1.get(ChronoField.MICRO_OF_SECOND): " + range);

        // Here, this method gets the valid 
        // range of values for the given field 
        // i.e. here the MILLI_OF_SECOND field 
        // range will be returned 
        range = (ValueRange) ins2.range(ChronoField.MILLI_OF_SECOND);

        // Display range
        System.out.println("ins2.get(ChronoField.MILLI_OF_SECOND): " + range);

        // Here, this method gets the valid 
        // range of values for the given field 
        // i.e. here the NANO_OF_SECOND field 
        // range will be returned 
        range = (ValueRange) ins2.range(ChronoField.NANO_OF_SECOND);

        // Display range
        System.out.println("ins2.get(ChronoField.NANO_OF_SECOND): " + range);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-27T05:30:00.259110Z

ins1.get(ChronoField.MICRO_OF_SECOND): 0 - 999999
ins2.get(ChronoField.MILLI_OF_SECOND): 0 - 999
ins2.get(ChronoField.NANO_OF_SECOND): 0 - 999999999



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.