MonthDay range() method in Java

Java 8Object Oriented ProgrammingProgramming

The range of values for a ChronoField can be obtained using the range() method in the MonthDay 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) {
      MonthDay md = MonthDay.parse("--02-21");
      System.out.println("The MonthDay is: " + md);
      ValueRange range = md.range(ChronoField.DAY_OF_MONTH);
      System.out.println("The range of DAY_OF_MONTH is: " + range);
   }
}

output

The MonthDay is: --02-21
The range of DAY_OF_MONTH is: 1 - 28/29

Now let us understand the above program.

First the MonthDay 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:

MonthDay md = MonthDay.parse("--02-21");
System.out.println("The MonthDay is: " + md);
ValueRange range = md.range(ChronoField.DAY_OF_MONTH);
System.out.println("The range of DAY_OF_MONTH is: " + range);
raja
Updated on 30-Jul-2019 22:30:25

Advertisements