Skip to content

[java] ReplaceJavaUtilDate: False negative when using pattern matching #7009

Closed

Description

@huxi

Affects PMD Version:
7.26.0

Rule:
ReplaceJavaUtilDate

Likely also the case for ReplaceJavaUtilCalendar

Description:

Using pattern variable suppresses violations.

Code Sample demonstrating the issue:

import java.time.LocalDateTime;
import java.time.ZoneId;

public final class PmdCanary {
    private PmdCanary() {
    }

    // no warnings
    //@SuppressWarnings("PMD.ReplaceJavaUtilDate")
    public static LocalDateTime toLocalDateTimePatternVariable(Object o) {
        if (o instanceof LocalDateTime localDateTime) {
            return localDateTime;
        }
        if (o instanceof java.sql.Timestamp timestamp) {
            return timestamp.toLocalDateTime();
        }
        if (o instanceof java.sql.Date sqlDate) {
            return sqlDate.toLocalDate().atStartOfDay();
        }
        if (o instanceof java.util.Date date) {
            return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        }

        return null;
    }

    @SuppressWarnings("PMD.ReplaceJavaUtilDate")
    public static LocalDateTime toLocalDateTime(Object o) {
        if (o instanceof LocalDateTime) {
            return (LocalDateTime) o;
        }
        if (o instanceof java.sql.Timestamp) {
            java.sql.Timestamp timestamp = (java.sql.Timestamp) o; // warns
            return timestamp.toLocalDateTime();
        }
        if (o instanceof java.sql.Date) {
            java.sql.Date sqlDate = (java.sql.Date) o; // warns
            return sqlDate.toLocalDate().atStartOfDay();
        }
        if (o instanceof java.util.Date) {
            java.util.Date date = (java.util.Date) o; // warns

            return date.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        }

        return null;
    }
}

Expected outcome:

PMD should report violations consistently for both versions of the method.

The version using pattern variables is currently not causing any violations.

I also agree with #6025 that java.sql classes should probably not trigger a violation at all.

Running PMD through: Gradle

Activity

  1. added
    a:false-negative
    on Aug 30, 2026
  2. added this to the 7.28.0 milestone on Aug 30, 2026
  3. added a commit that references this issue on Aug 30, 2026
    9c1fe8f
  4. changed the title [-][java] ReplaceJavaUtilDate is suppressed by using pattern variable[/-] [+][java] ReplaceJavaUtilDate: False negative when using pattern matching[/+] on Aug 30, 2026
  5. huxi commented on Aug 30, 2026

    @huxi
    Author

    Now that was super fast! Thanks a lot! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions