A DESCRIPTION OF THE PROBLEM :
The specification of java.sql.Timestamp.hashCode explicitly states that nanos not be included in its computation. However, the current implementation returns (int)(this.getTime()^(this.getTime() >>> 32)), and since Timestamp overrides getTime() to include nanos/1_000_000 in the returned millisecond value, the hash code is indirectly affected by the nanos field.
Original documentation: "The hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation."
---------- BEGIN SOURCE ----------
import java.sql.Timestamp;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TimestampTest {
@Test
public void testHashCodeConsistency() {
long t = 1000;
Timestamp ts1 = new Timestamp(t);
Timestamp ts2 = new Timestamp(t);
ts1.setNanos(123456789);
ts2.setNanos(987654321);
// Verify hashCodes are equal despite different nanos values
assertEquals(ts1.hashCode(), ts2.hashCode());
}
}
---------- END SOURCE ----------
The specification of java.sql.Timestamp.hashCode explicitly states that nanos not be included in its computation. However, the current implementation returns (int)(this.getTime()^(this.getTime() >>> 32)), and since Timestamp overrides getTime() to include nanos/1_000_000 in the returned millisecond value, the hash code is indirectly affected by the nanos field.
Original documentation: "The hashCode method uses the underlying java.util.Date implementation and therefore does not include nanos in its computation."
---------- BEGIN SOURCE ----------
import java.sql.Timestamp;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class TimestampTest {
@Test
public void testHashCodeConsistency() {
long t = 1000;
Timestamp ts1 = new Timestamp(t);
Timestamp ts2 = new Timestamp(t);
ts1.setNanos(123456789);
ts2.setNanos(987654321);
// Verify hashCodes are equal despite different nanos values
assertEquals(ts1.hashCode(), ts2.hashCode());
}
}
---------- END SOURCE ----------