MonthDay range() method in Java with Examples
Last Updated :
28 Jan, 2019
range() method of the MonthDay class used to get the ValueRange object which is the range of field in terms of the minimum and maximum values for the field passed as a parameter to this method. The method returns ValueRange object only for those fields which are supported by MonthDay object. So when the field is not supported by this method then an exception is thrown by this method.
Syntax:
public ValueRange range(TemporalField field)
Parameters: This method accepts one parameter field which is the field to query the range.
Return value: This method returns the range of valid values for the field.
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:
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
MonthDay monthDay
= MonthDay.parse( "--12-06" );
ValueRange result
= monthDay.range(ChronoField.MONTH_OF_YEAR);
System.out.println( "Range in MONTH_OF_YEAR: "
+ result);
}
}
|
Output:
Range in MONTH_OF_YEAR: 1 - 12
Program 2:
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
MonthDay monthDay
= MonthDay.parse( "--12-06" );
ValueRange result
= monthDay.range(ChronoField.DAY_OF_MONTH);
System.out.println( "Range in DAY_OF_MONTH: "
+ result);
}
}
|
Output:
Range in DAY_OF_MONTH: 1 - 31
References: https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#range(java.time.temporal.TemporalField)
Share your thoughts in the comments
Please Login to comment...