Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

LocalTime range() method in Java with Examples

  • Last Updated : 28 Jan, 2019

range() method of the LocalTime class is used to get the range of field in terms of the minimum and maximum values and the field is passed as a parameter to this method. The returned value for this method is ValueRange object for the field and the method returns ValueRange object only for those fields which are supported by LocalTime object. So when the field is not supported by this method then an exception is thrown by this method.

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:




// Java program to demonstrate
// LocalTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalTime objects
        LocalTime l1
            = LocalTime
                  .parse("09:21:12");
  
        // apply range() method of LocalTime class
        ValueRange result
            = l1.range(ChronoField.MILLI_OF_SECOND);
  
        // print results
        System.out.println("Range in MILLI_OF_SECOND: "
                           + result);
    }
}
Output:
Range in MILLI_OF_SECOND: 0 - 999

Program 2:




// Java program to demonstrate
// LocalTime.range() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create LocalTime objects
        LocalTime l1
            = LocalTime
                  .parse("19:21:12");
  
        // apply range() method of LocalTime class
        ValueRange result
            = l1.range(ChronoField.MINUTE_OF_HOUR);
  
        // print results
        System.out.println("Range in MINUTE_OF_HOUR: "
                           + result);
    }
}
Output:
Range in MINUTE_OF_HOUR: 0 - 59

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#range(java.time.temporal.TemporalField)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!