A month, day, hour, minute or second is set to a number outside its range

tx0/java:out-of-range-date-fielddefaulterrorbug

What it finds

calendar.set(Calendar.MONTH, 12) asks for a thirteenth month: Calendar numbers months from 0, and since it is lenient by default it silently moves on to January of the next year. LocalDate.of(2024, 13, 1) and LocalTime.of(24, 0) throw a DateTimeException instead.

Reported is an integer literal outside the range of its field: for Calendar.set with one of the fields MONTH, DAY_OF_MONTH, DAY_OF_WEEK, HOUR_OF_DAY, HOUR, MINUTE, SECOND and MILLISECOND, and for the month, day, hour, minute and second given to of of LocalDate, LocalTime, LocalDateTime, YearMonth and MonthDay.

From the rule's source, out_of_range_date_field.rs.

Example from the rule's tests
import java.time.LocalDate;import java.time.LocalTime;import java.util.Calendar; class Dates {  void set(Calendar calendar) {    calendar.set(Calendar.MONTH, 12);tx0 reports: Calendar.MONTH runs from 0 to 11, not 12    calendar.set(Calendar.MONTH, 11);    calendar.set(Calendar.HOUR_OF_DAY, 24);tx0 reports: runs from 0 to 23, not 24    LocalDate a = LocalDate.of(2024, 13, 1);tx0 reports: the month runs from 1 to 12, not 13    LocalDate b = LocalDate.of(2024, 12, 31);    LocalTime c = LocalTime.of(10, 60);tx0 reports: the minute runs from 0 to 59, not 60    LocalTime d = LocalTime.of(23, 59);  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:out-of-range-date-field" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:out-of-range-date-field] reason="..."

tx0 explain tx0/java:out-of-range-date-field shows the same in the terminal. Found a false positive? Report it.