The java.time Bugs That Donât Throw Exceptions
Blog post from Sonar
SonarQube's new java.time rules aim to address subtle bugs in Java programming that can lead to incorrect calculations, particularly in the realm of time zones and duration math. These issues often arise when using LocalDateTime for duration calculations across different time zones, leading to plausible yet incorrect results that can affect billing, SLA, and scheduling systems. The rules, such as S8700, flag problematic uses like Duration.between() with LocalDateTime operands, suggesting the use of ZonedDateTime to incorporate time zone differences accurately. Rule S8220 helps prevent runtime exceptions by catching improper LocalDateTime to Instant conversions, while S8692 emphasizes the need for a fixed Clock in tests to ensure deterministic results. Additionally, S8696 addresses the misuse of == for value-based types like Instant, where equals should be used instead. These rules, part of SonarQube's default settings on the cloud and set to ship on the server in version 2026.4, aim to mitigate errors that can pass unnoticed through tests but cause significant downstream impacts.