This is an issue when a smaller integer type value is widened to a larger integer type and then used as an absolute timestamp, such as milliseconds or seconds since the Unix epoch (January 1, 1970).
A 32-bit signed integer can hold values from -2,147,483,648 to 2,147,483,647. While this might seem like a large range, it’s insufficient for representing timestamps:
When you cast a 32-bit integer to a 64-bit integer for use as a timestamp, you’re not fixing the underlying problem — the value is already corrupted or limited by the 32-bit constraint before the cast happens. The cast simply preserves the incorrect or overflowed value in a larger container.
Timestamps should be stored as 64-bit integer values from the start to ensure they can represent dates far into the past and future without overflow. A 64-bit integer type can represent timestamps for approximately 292 million years, which is more than sufficient for any practical application.
Using a 32-bit integer for timestamps is almost always a bug that will cause:
In Java, 32-bit integers are represented by the int type and 64-bit integers by the long type.
Using 32-bit signed integer types for timestamps can lead to serious reliability issues:
Change the variable type from int to long to properly represent the timestamp. This ensures the value can hold the full
range of possible timestamp values without overflow.
int timestamp = 1234567890; Date date = new Date(timestamp); // Noncompliant: int implicitly widened Date date2 = new Date((long) timestamp); // Noncompliant: the cast does not fix the overflow
long timestamp = 1234567890L; Date date = new Date(timestamp);