ADVERTISEMENT
ADVERTISEMENT

Java LocalDate Class | range() Method with Example

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

LocalDate 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 LocalDate.

Example:

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

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

public class RangeOfLocalDate {
    public static void main(String args[]) {
        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);

        // Display l_da1,l_da2 
        System.out.println("LocalDate l_da1,l_da2 : ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);

        System.out.println();

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

        // Display range
        System.out.println("l_da1.range(ChronoField.DAY_OF_WEEK): " + range);

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

        // Display range
        System.out.println("l_da2.range(ChronoField.YEAR): " + range);

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

        // Display range
        System.out.println("l_da1.range(ChronoField.MONTH_OF_YEAR): " + range);
    }
}

Output

LocalDate l_da1,l_da2 : 
l_da1: 2007-04-04
l_da2: 2008-02-06

l_da1.range(ChronoField.DAY_OF_WEEK): 1 - 7
l_da2.range(ChronoField.YEAR): -999999999 - 999999999
l_da1.range(ChronoField.MONTH_OF_YEAR): 1 - 12
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.