Java LocalDateTime range() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
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
CallWhat 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
// java.time.LocalDateTime
public ValueRange range(TemporalField field)

Parameters

ParameterDescription
field (TemporalField)The field to ask about, such as ChronoField.DAY_OF_MONTH.

Return Value

Return ValueDescription
ValueRangeAn object that holds the minimum and maximum valid values of the field.

Compare with Similar Methods

Related methods:

MethodHow it differs
isSupported(field)Checks if a field can be used at all.
get(field)Reads the current value of the field.
withDayOfMonth(d)Sets a day that must be inside the range.

Java Examples

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.

Java
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));
    }
}

How It Works

  1. The range depends on the date. Each date-time answers for its own month.
  2. February 2026. The days go from 1 to 28.
  3. February 2024. It is a leap year, so the days go up to 29.
  4. 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.

Java
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));
    }
}

How It Works

  1. Hours. The valid hours are 0 to 23.
  2. Minutes. The valid minutes are 0 to 59.
  3. 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.

Java
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));
    }
}

How It Works

  1. Two years. The first has 365 days and the second, a leap year, has 366.
  2. getMaximum(). It reads the largest valid day number from each range.
  3. isValidValue(). It answers if day 366 can be used.
  4. 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

Use dt.range(ChronoField.DAY_OF_MONTH).getMaximum(), or dt.toLocalDate().lengthOfMonth().
The minimum and the maximum separated by a dash, such as 1 - 28.
2024 is a leap year with 366 days. 2026 has 365 days.
Yes, an UnsupportedTemporalTypeException if the field does not belong to a LocalDateTime, such as OFFSET_SECONDS.
From -999999999 to 999999999, which are the limits of the years that java.time supports.

Next: toLocalDate()

Get only the date part, without the time.

toLocalDate() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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