-
Bug
-
Resolution: Fixed
-
P4
-
None
-
b16
Consider the program below. The spec strongly suggests that IllegalArgumentException should be thrown by getDisplayName and getDisplayNames whenever the calendar is in non-lenient mode and has invalid fields. But only one of the 4 cases below does so. Furthermore, when an exception is thrown, it is misleading - the passed in field is not invalid, it's the internal state. IllegalStateException would have been better, but it's 20 years too late... Probably the spec should be updated to better reflect actual behavior.
$ cat GetDisplayName.java; echo ---; java GetDisplayName
import java.util.Calendar;
import java.util.Locale;
import static java.util.Calendar.*;
public class GetDisplayName {
public static void main(String[] args) throws Throwable {
Calendar c = Calendar.getInstance(Locale.US);
c.setLenient(false);
c.set(SECOND, 9999); // invalid!
for (int field : new int[] { SECOND, MONTH }) {
try {
System.out.println(c.getDisplayName(field, SHORT, Locale.US));
} catch (Throwable t) { t.printStackTrace(); }
try {
System.out.println(c.getDisplayNames(field, SHORT, Locale.US));
} catch (Throwable t) { t.printStackTrace(); }
}
}
}
---
null
null
java.lang.IllegalArgumentException: SECOND
at java.base/java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2644)
at java.base/java.util.Calendar.updateTime(Calendar.java:3395)
at java.base/java.util.Calendar.complete(Calendar.java:2267)
at java.base/java.util.Calendar.get(Calendar.java:1826)
at java.base/java.util.Calendar.getDisplayName(Calendar.java:2087)
at GetDisplayName.main(GetDisplayName.java:12)
$ cat GetDisplayName.java; echo ---; java GetDisplayName
import java.util.Calendar;
import java.util.Locale;
import static java.util.Calendar.*;
public class GetDisplayName {
public static void main(String[] args) throws Throwable {
Calendar c = Calendar.getInstance(Locale.US);
c.setLenient(false);
c.set(SECOND, 9999); // invalid!
for (int field : new int[] { SECOND, MONTH }) {
try {
System.out.println(c.getDisplayName(field, SHORT, Locale.US));
} catch (Throwable t) { t.printStackTrace(); }
try {
System.out.println(c.getDisplayNames(field, SHORT, Locale.US));
} catch (Throwable t) { t.printStackTrace(); }
}
}
}
---
null
null
java.lang.IllegalArgumentException: SECOND
at java.base/java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2644)
at java.base/java.util.Calendar.updateTime(Calendar.java:3395)
at java.base/java.util.Calendar.complete(Calendar.java:2267)
at java.base/java.util.Calendar.get(Calendar.java:1826)
at java.base/java.util.Calendar.getDisplayName(Calendar.java:2087)
at GetDisplayName.main(GetDisplayName.java:12)
- csr for
-
JDK-8304371 Calendar.getDisplayName(s) in non-lenient mode inconsistent, does not match spec
- Closed