Join our Newsletter — 33% off our NHI Course

LocalDateTime arithmetic: what it means for reliable time handling

 

(@nhi-mgmt-group)
Member Moderator
Joined: 1 year ago
Posts: 16521
Topic starter  

TL;DR: Java.time bugs often compile, pass tests, and still return silently wrong answers, especially when LocalDateTime is used across timezones or when tests call now() without an injected Clock, according to Sonar. The risk is not just correctness drift but downstream billing, SLA, and scheduling errors that look plausible enough to survive review.

Editorial analysis by NHI Mgmt Group, based on content published by Sonar: “The java.time bugs that don’t throw exceptions”.

Key questions

Q: What breaks when LocalDateTime is used for elapsed time across timezones?

A: The calculation returns a calendar-style difference instead of true elapsed time, so the result can look plausible while being wrong.

Q: Why do time-based tests become unreliable without an injected Clock?

A: Without a controllable Clock, tests depend on JVM speed, clock resolution, and runtime conditions that vary across machines.

Q: How do teams know when time handling is failing in production code?

A: Look for business logic that depends on elapsed time but uses local wall-clock types, especially where values still appear reasonable in review.

Practitioner guidance

  • Use timezone-aware timestamps for elapsed time Replace LocalDateTime duration math with ZonedDateTime or Instant whenever the calculation crosses systems, regions, or user locales.
  • Inject a controllable Clock into time-dependent code Refactor code that calls now() so tests can supply a fixed Clock and assert reproducible results.
  • Ban identity comparison on value-based time objects Review code for == and identityHashCode usage on Instant, LocalDateTime, Optional, and boxed primitives.

Bottom line: LocalDateTime arithmetic can return believable but incorrect elapsed times when timestamps cross timezone boundaries, which makes the defect easy to miss in review.

Explore further

View Full Forum →  |  NHI Foundation Course →  |  Our Services →  |  Read the full analysis →



   
Quote
(@mr-nhi)
Member Moderator
Joined: 5 months ago
Posts: 17012
 

Silent time correctness debt is a governance issue, not just a coding issue. When a codebase normalises LocalDateTime for elapsed-time logic or lets tests depend on wall-clock time, the programme is accepting hidden nondeterminism. That matters because the defect is structurally hard to detect after the fact. Practitioners should treat time handling as a control boundary, not a convenience API choice.

A question worth separating out:

Q: What is the difference between value equality and object identity for time objects?

A: Value equality asks whether two time objects represent the same moment. Object identity asks whether they are the same instance in memory, which is irrelevant for value-based classes like Instant. In reliable time logic, identity comparison can be accidentally false even when the timestamps are semantically equal.

👉 Read our full editorial: Silent time bugs in java.time expose the limits of local dates



   
ReplyQuote
Share:

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.