LocalTime range() method in Java


The range of values for a ChronoField can be obtained using the range() method in the LocalTime class in Java. This method requires a single parameter i.e. the ChronoField for which the range of values is required and it returns the range of values.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
public class Main {
   public static void main(String[] args) {
      LocalTime lt = LocalTime.parse("11:19:50");
      System.out.println("The LocalTime is: " + lt);
      ValueRange range = lt.range(ChronoField.MICRO_OF_SECOND);
      System.out.println("The range of MICRO_OF_SECOND is: " + range);
   }
}

Output

The LocalTime is: 11:19:50
The range of MICRO_OF_SECOND is: 0 - 999999

Now let us understand the above program.

First the LocalTime is displayed. Then the range of values of a particular ChronoField are obtained using the range() method and displayed. A code snippet that demonstrates this is as follows −

LocalTime lt = LocalTime.parse("11:19:50");
System.out.println("The LocalTime is: " + lt);
ValueRange range = lt.range(ChronoField.MICRO_OF_SECOND);
System.out.println("The range of MICRO_OF_SECOND is: " + range);

Updated on: 30-Jul-2019

66 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements