Java ChronoLocalDate range(TemporalField field)

PreviousNext

The range() method of a ChronoLocalDate interface gets the range of valid values for the field passes.

This method returns a ValueRange object which contains the minimum and maximum valid values for a field.

When the field is not supported then an exception is thrown.

This ChronoLocalDate can enhance the accuracy of the returned range.

Syntax:

public ValueRange range(TemporalField field)

Parameter: field which is the field to query the range for.

Return Value: This method returns ValueRange which is the range of valid values for the field.

Example

The following code shows how to use range() method:

// Java program to demonstrate
// ChronoLocalDate.range() method

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

public class Main {
    public static void main(String[] args) {

        // create a ChronoLocalDate object
        ChronoLocalDate localD = LocalDate.parse("2018-12-06");

        // print ChronoLocalDate
        System.out.println("ChronoLocalDate: " + localD);

        // get range of Days field
        // from ChronoLocalDate using range method
        ValueRange range = localD.range(ChronoField.DAY_OF_MONTH);

        // print range of DAY_OF_MONTH
        System.out.println("Range of DAY_OF_MONTH: " + range);
    }//    w w  w   .d    em    o 2   s.    c  o  m
}

Result

Another example:

Java program to demonstrate ChronoLocalDate.range() method

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

public class Main {
    public static void main(String[] args) {

        // create a ChronoLocalDate object
        ChronoLocalDate localD = LocalDate.parse("2018-12-06");

        // print ChronoLocalDate
        System.out.println("ChronoLocalDate: " + localD);

        // get range of DAY_OF_YEAR field
        // from ChronoLocalDate using range method
        ValueRange range = localD.range(ChronoField.DAY_OF_YEAR);

        // print range of DAY_OF_YEAR
        System.out.println("Range of DAY_OF_YEAR: " + range);
    }/* w w   w   .d  e  m    o 2   s.  c  o   m */
}

Result

PreviousNext

Related