Monday, November 18, 2024
Google search engine
HomeLanguagesJavaChronoLocalDateTime range() method in Java with Examples

ChronoLocalDateTime range() method in Java with Examples

The range() method of a ChronoLocalDateTime interface is used to get the range of valid values for the field passes as a parameter. This method returns a ValueRange object which contains the minimum and maximum valid values for a field. This ChronoLocalDateTime is helpful to enhance the accuracy of the returned range. When the field is not supported and the method is unable to return range values then an exception is thrown.

Syntax:

default ValueRange range(TemporalField field)

Parameters: This method accepts one single parameter field which is the field to get range of values.

Return value: This method returns ValueRange which is the range of valid values for the field, not null.

Exception: This method throws following Exceptions:

  • DateTimeException – if the range for the field cannot be obtained.
  • UnsupportedTemporalTypeException – if the field is not supported.

Below programs illustrate the range() method:

Program 1:




// Java program to demonstrate
// ChronoLocalDateTime.range() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ChronoLocalDateTime object
        ChronoLocalDateTime localDT
            = LocalDateTime
                  .parse("2018-10-25T23:12:31.123");
  
        // print ChronoLocalDateTime
        System.out.println("ChronoLocalDateTime of Calcutta: "
                           + localDT);
  
        // get range of MILLI_OF_SECOND field
        // from ChronoLocalDateTime using range method
        ValueRange range
            = localDT.range(ChronoField.MILLI_OF_SECOND);
  
        // print range of MILLI_OF_SECOND
        System.out.println("Range of MILLI_OF_SECOND: "
                           + range);
    }
}


Output:

ChronoLocalDateTime of Calcutta: 2018-10-25T23:12:31.123
Range of MILLI_OF_SECOND: 0 - 999

Program 2:




// Java program to demonstrate
// ChronoLocalDateTime.range() method
  
import java.time.*;
import java.time.chrono.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ChronoLocalDateTime object
        ChronoLocalDateTime localDT
            = LocalDateTime
                  .parse("2019-12-31T19:15:30");
  
        // print ChronoLocalDateTime
        System.out.println("ChronoLocalDateTime of Calcutta: "
                           + localDT);
  
        // get range of NANO_OF_SECOND field
        // from localDateTime using range method
        ValueRange range
            = localDT.range(ChronoField.NANO_OF_SECOND);
  
        // print range of NANO_OF_SECOND
        System.out.println("Range of NANO_OF_SECOND: "
                           + range);
    }
}


Output:

ChronoLocalDateTime of Calcutta: 2019-12-31T19:15:30
Range of NANO_OF_SECOND: 0 - 999999999

Reference: https://docs.oracle.com/javase/9/docs/api/java/time/temporal/TemporalAccessor.html#range-java.time.temporal.TemporalField-

RELATED ARTICLES

Most Popular

Recent Comments