Temporal ValueRange Checks Calendar Bounds
Query LocalDate.range(ChronoField.DAY_OF_MONTH) to inspect month-specific ValueRange bounds and validate candidate days before construction.
Code
import java.time.DateTimeException;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
public class CalendarRangeGuard {
public static void main(String[] args) {
ValueRange genericDom = ChronoField.DAY_OF_MONTH.range();
System.out.println("generic DOM: " + genericDom
+ " fixed=" + genericDom.isFixed()
+ " smallestMax=" + genericDom.getSmallestMaximum());
ValueRange leapFeb = LocalDate.of(2024, 2, 1).range(ChronoField.DAY_OF_MONTH);
ValueRange commonFeb = LocalDate.of(2026, 2, 1).range(ChronoField.DAY_OF_MONTH);
System.out.println("2024-02 valid 29: " + leapFeb.isValidIntValue(29));
System.out.println("2026-02 valid 29: " + commonFeb.isValidIntValue(29));
System.out.println("2026-02 bounds: " + commonFeb.getMinimum() + ".." + commonFeb.getMaximum());
try {
commonFeb.checkValidIntValue(29, ChronoField.DAY_OF_MONTH);
} catch (DateTimeException ex) {
System.out.println("rejected day 29: " + ex.getMessage());
}
}
}Logic trace — Temporal ValueRange Checks Calendar Bounds
Under the hood: the class CalendarRangeGuard and the function main.
Watch out for
- Copy, run: the standard library answers every call in the file.
Putting it to use
Primary entry header declares public static void main(String[] args)
Output lines are prefixed (generic DOM:, 2024-02 valid 29:, 2026-02 valid 29:), displaying intermediate results explicitly inside printed console lines.
Core lines
ValueRange commonFeb = LocalDate.of(2026, 2, 1).range(ChronoField.DAY_OF_MONTH);
ValueRange leapFeb = LocalDate.of(2024, 2, 1).range(ChronoField.DAY_OF_MONTH);
commonFeb.checkValidIntValue(29, ChronoField.DAY_OF_MONTH);
Interactive Tools for This Topic
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Age CalculatorCalculate exact age in years, months, and days between a birth date and a reference date, plus total days and the next birthday countdown.
FAQ
- What does Temporal ValueRange Checks Calendar Bounds print when run?
- The script runs standalone; its 5 labeled outputs include `generic DOM:`, `2024-02 valid 29:`, `2026-02 valid 29:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.