org.sonar.l10n.java.rules.squid.S2110.html Maven / Gradle / Ivy
The newest version!
Whether the valid value ranges for Date
fields start with 0 or 1 varies by field. For instance, month starts at 0, and day of month starts at 1. Enter a date value that goes past the end of the valid range, and the date will roll without error or exception. For instance, enter 12 for month, and you'll get January of the following year.
This rule checks for bad values used in conjunction with java.util.Date
, java.sql.Date
, and java.util.Calendar
. Specifically, values outside of the valid ranges:
Field Valid
month 0-11
date (day) 0-31
hour 0-23
minute 0-60
second 0-61
Note that this rule does not check for invalid leap years, leap seconds (second = 61), or invalid uses of the 31st day of the month.
Noncompliant Code Example
Date d = new Date();
d.setDate(25);
d.setYear(2014);
d.setMonth(12); // Noncompliant; rolls d into the next year
Calendar c = new GregorianCalendar(2014, 12, 25); // Noncompliant
if (c.get(Calendar.MONTH) == 12) { // Noncompliant; invalid comparison
// ...
}
Compliant Solution
Date d = new Date();
d.setDate(25);
d.setYear(2014);
d.setMonth(11);
Calendar c = new Gregorian Calendar(2014, 11, 25);
if (c.get(Calendar.MONTH) == 11) {
// ...
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy