Home Java LocalDateTime Methods range() Java LocalDateTime range() Method Definition In Java, range(field) returns a ValueRange that tells you the smallest and the largest value a field can have for this particular date-time.
The range depends on the date. For example, the range of DAY_OF_MONTH is 1 to 28 in February 2026, 1 to 29 in February 2024, and 1 to 31 in March.
An instance method of java.time.LocalDateTime (since Java 8): call it on a LocalDateTime object. Add import java.time.LocalDateTime; at the top of your file. It comes from TemporalAccessor, which LocalDateTime implements.
Syntax Pass a ChronoField, then ask the result for its limits.
LocalDateTime dt = LocalDateTime.of(2026, 2, 10, 14, 30);
ValueRange days = dt.range(ChronoField.DAY_OF_MONTH);
long smallest = days.getMinimum(); // 1
long largest = days.getMaximum(); // 28 Call What it does dt.range(field)Returns a ValueRange for the field. range.getMinimum()The smallest valid value. range.getMaximum()The largest valid value. range.isValidValue(v)Checks whether v is inside the range.
Printing a ValueRange shows the limits as text, such as 1 - 28.
Library declarations // java.time.LocalDateTime
public ValueRange range(TemporalField field) Parameters Parameter Description field (TemporalField)The field to ask about, such as ChronoField.DAY_OF_MONTH.
Return Value Return Value Description ValueRangeAn object that holds the minimum and maximum valid values of the field.
Important
The range is worked out for this date-time, so the answer for DAY_OF_MONTH or DAY_OF_YEAR changes with the month and the year. A field that the date-time does not support throws an UnsupportedTemporalTypeException. The limits are long values, because some ranges are very large.
Compare with Similar Methods Related methods:
Java Examples 1. Days of the month 2. Time fields 3. Use the limits Example 1 — Find How Many Days a Month Has Read the range of DAY_OF_MONTH for February in a normal year, February in a leap year, and March.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LdtRangeDays {
public static void main(String[] args) {
LocalDateTime feb2026 = LocalDateTime.of(2026, 2, 10, 12, 0);
LocalDateTime feb2024 = LocalDateTime.of(2024, 2, 10, 12, 0);
LocalDateTime march = LocalDateTime.of(2026, 3, 10, 12, 0);
System.out.println("Feb 2026: " + feb2026.range(ChronoField.DAY_OF_MONTH));
System.out.println("Feb 2024: " + feb2024.range(ChronoField.DAY_OF_MONTH));
System.out.println("Mar 2026: " + march.range(ChronoField.DAY_OF_MONTH));
}
} Feb 2026: 1 - 28
Feb 2024: 1 - 29
Mar 2026: 1 - 31 How It Works The range depends on the date. Each date-time answers for its own month.February 2026. The days go from 1 to 28.February 2024. It is a leap year, so the days go up to 29.March. The days go from 1 to 31.Example 2 — Read the Ranges of Time Fields Time fields have the same range on every date.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
public class LdtRangeTime {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.of(2026, 3, 15, 14, 30);
System.out.println("HOUR_OF_DAY: " + dt.range(ChronoField.HOUR_OF_DAY));
System.out.println("MINUTE_OF_HOUR: " + dt.range(ChronoField.MINUTE_OF_HOUR));
System.out.println("NANO_OF_SECOND: " + dt.range(ChronoField.NANO_OF_SECOND));
}
} HOUR_OF_DAY: 0 - 23
MINUTE_OF_HOUR: 0 - 59
NANO_OF_SECOND: 0 - 999999999 How It Works Hours. The valid hours are 0 to 23.Minutes. The valid minutes are 0 to 59.Nanoseconds. The fraction of a second goes from 0 to 999,999,999.Example 3 — Use the Limits in Code Ask the range of DAY_OF_YEAR for a normal year and a leap year, and test if day 366 is valid.
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
public class LdtRangeLimits {
public static void main(String[] args) {
LocalDateTime normal = LocalDateTime.of(2026, 6, 1, 8, 0);
LocalDateTime leap = LocalDateTime.of(2024, 6, 1, 8, 0);
ValueRange normalRange = normal.range(ChronoField.DAY_OF_YEAR);
ValueRange leapRange = leap.range(ChronoField.DAY_OF_YEAR);
System.out.println("2026 largest day: " + normalRange.getMaximum());
System.out.println("2024 largest day: " + leapRange.getMaximum());
System.out.println("day 366 valid in 2026: " + normalRange.isValidValue(366));
System.out.println("day 366 valid in 2024: " + leapRange.isValidValue(366));
}
} 2026 largest day: 365
2024 largest day: 366
day 366 valid in 2026: false
day 366 valid in 2024: true How It Works Two years. The first has 365 days and the second, a leap year, has 366.getMaximum(). It reads the largest valid day number from each range.isValidValue(). It answers if day 366 can be used.The result. Day 366 is not valid in 2026 but valid in 2024, which also tells you when withDayOfYear(366) would fail.Frequently Asked Questions How do I find the number of days in a month with LocalDateTime? Use dt.range(ChronoField.DAY_OF_MONTH).getMaximum(), or dt.toLocalDate().lengthOfMonth().
What does a ValueRange print as? The minimum and the maximum separated by a dash, such as 1 - 28.
Why is the range of DAY_OF_YEAR different for 2024 and 2026? 2024 is a leap year with 366 days. 2026 has 365 days.
Can range() throw an exception? Yes, an UnsupportedTemporalTypeException if the field does not belong to a LocalDateTime, such as OFFSET_SECONDS.
What is the range of YEAR? From -999999999 to 999999999, which are the limits of the years that java.time supports.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion