| CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix #2862: [java] Add rules discouraging the use of java.util.Calendar and java.util.Date #5883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Compared to main: (comment created at 2025-07-12 20:17:41+00:00 for 591af28) |
* java/errorprone/ReplaceJavaUtilCalendar * java/errorprone/ReplaceJavaUtilDate
|
@adangel Is there anything I still need to do, if I want to have this merged? |
adangel
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Can you fix my small suggestions? Other than that, it looks good!
While looking through the regression report, I found, that it reports multiple violations in this kind of code:
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault()); // first violation
cal.setTimeInMillis(timeNow);
cal.set(Calendar.SECOND, 0); // second violation
cal.set(Calendar.MILLISECOND, 0); // third
cal.set(Calendar.HOUR_OF_DAY, mm); // fourth
cal.set(Calendar.MINUTE, mm); // fifthIs this ok?
@oowekyala @jsotuyod Any thoughts on the naming? Does "ReplaceJavaUtilCalendar" and "ReplaceJavaUtilDate" sound good?
...src/test/java/net/sourceforge/pmd/lang/java/rule/errorprone/ReplaceJavaUtilCalendarTest.java
Outdated
Show resolved
Hide resolved
...ava/src/test/java/net/sourceforge/pmd/lang/java/rule/errorprone/ReplaceJavaUtilDateTest.java
Outdated
Show resolved
Hide resolved
...test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ReplaceJavaUtilCalendar.xml
Show resolved
Hide resolved
...src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/ReplaceJavaUtilDate.xml
Show resolved
Hide resolved
|
I'm not attached to the names, so if you have better suggestions... |
|
About the multiple warnings in one file: Each of them is valid on their own. Is there a downside to having a rule trigger multiple times in a file? |
There is no problem with multiple violations. We only should take care, that we report each problem only once (like you said: each should be a valid violation on its own), to avoid unnecessary violations. Since this rule is about discouraging the use of Calendar - the fix would be to use e.g. ZonedDateTime, which makes all the violations go away in the code block at once. So, calling the constructor of |
|
Something like this? I personally would prefer multiple violations over false negatives. But I think this won't miss much, so I'm fine with this version. |
Describe the PR
Add rules discouraging the use of java.util.Calendar and java.util.Date. Instead, one should use the classes in java.time.
Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)