Signup/Sign In
LAST UPDATED: NOVEMBER 27, 2020

Java LocalDate range() Method

Java range() method is used to get the range of valid values for the specified field. The range value expresses the minimum and maximum valid values for a field. For example, if we want to get the number of days in the month of October then we get a range 1-31 for this month.

We can use ChronoField enum as an argument to get a range of the specified field. The ChronoField enum has the following values:

Field Description

ALIGNED_DAY_OF_WEEK_IN_MONTH

The aligned day-of-week within a month.

ALIGNED_DAY_OF_WEEK_IN_YEAR

The aligned day-of-week within a year.

ALIGNED_WEEK_OF_MONTH

The aligned week within a month.

ALIGNED_WEEK_OF_YEAR

The aligned week within a year.

AMPM_OF_DAY

The am-pm-of-day.

CLOCK_HOUR_OF_AMPM

The clock-hour-of-am-pm.

CLOCK_HOUR_OF_DAY

The clock-hour-of-day.

DAY_OF_MONTH

The day-of-month.

DAY_OF_WEEK

The day-of-week, such as Tuesday.

DAY_OF_YEAR

The day-of-year.

EPOCH_DAY

The epoch-day, based on the Java epoch of 1970-01-01 (ISO).

ERA

The era.

HOUR_OF_AMPM

The hour-of-am-pm.

HOUR_OF_DAY

The hour-of-day.

INSTANT_SECONDS

The instant epoch-seconds.

MICRO_OF_DAY

The micro-of-day.

MICRO_OF_SECOND

The micro-of-second.

MILLI_OF_DAY

The milli-of-day.

MILLI_OF_SECOND

The milli-of-second.

MINUTE_OF_DAY

The minute-of-day.

MINUTE_OF_HOUR

The minute-of-hour.

MONTH_OF_YEAR

The month-of-year, such as March.

NANO_OF_DAY

The nano-of-day.

NANO_OF_SECOND

The nano-of-second.

OFFSET_SECONDS

The offset from UTC/Greenwich.

PROLEPTIC_MONTH

The proleptic-month based, counting months sequentially from year 0.

SECOND_OF_DAY

The second-of-day.

SECOND_OF_MINUTE

The second-of-minute.

YEAR

The proleptic year, such as 2012.

YEAR_OF_ERA

The year within the era.

Syntax

public ValueRange range(TemporalField field)

Parameters:

field - the field to query the range for.

Returns:

It returns the range of valid values for the field.

Time for an Example:

Let's take an example to get the range of the month in the date. We are using ChronoField enum to get a range by using the range() method.

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2012, 02, 29);
		System.out.println(localDate);
		ValueRange vr = localDate.range(ChronoField.DAY_OF_MONTH);
		System.out.println("Range : "+vr);
	}
}


2012-02-29
Range : 1 - 29

Time for another Example:

Let's take another example to understand the range() method. Here, we are getting range of the year in the date and getting range from 1 to 366.

import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange; 
public class DateDemo {
	
	public static void main(String[] args){  
		
		LocalDate localDate = LocalDate.of(2012, 02, 29);
		System.out.println(localDate);
		ValueRange vr = localDate.range(ChronoField.DAY_OF_YEAR);
		System.out.println("Range : "+vr);
	}
}


2012-02-29
Range : 1 - 366

Live Example:

Try with a live example, execute the code instantly with our powerful Online Java Compiler.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.