I came across this issue while writing a test case where I had to get a range of records between a range of timestamps –– using H2
embedded database with spring-data-jpa
.
The original issue is located at: Fetching records BETWEEN two java.time.Instant instances in Spring Data Query
I have the timestamps as java.time.Instant
instances.
If the user gives no start-end timestamps, I go ahead and plug in Instant.MIN
and Instant.MAX
respectively.
What perplexes me is that the following test-case passes:
@Test
public void test_date_min_max_instants_timestamps() {
Timestamp past = new Timestamp(Long.MIN_VALUE);
Timestamp future = new Timestamp(Long.MAX_VALUE);
Timestamp present = Timestamp.from(Instant.now());
assertTrue(present.after(past));
assertTrue(future.after(past));
assertTrue(future.after(present));
assertTrue(present.before(future));
assertTrue(past.before(present));
assertTrue(past.before(future));
}
but, the following test-case fails:
@Test
public void test_instant_ranges() throws InterruptedException {
Timestamp past = Timestamp.from(Instant.MIN);
Timestamp future = Timestamp.from(Instant.MAX);
Timestamp present = Timestamp.from(Instant.now());
assertTrue(present.after(past));
assertTrue(future.after(past));
assertTrue(future.after(present));
assertTrue(present.before(future));
assertTrue(past.before(present));
assertTrue(past.before(future));
}
Furthermore, if the past
and future
are not MIN/MAX values, but normal values instead, the result is as expected.
Any idea why java.sql.Timestamp behaves like this?
Also, if the time represented by Instant is too big for Timestamp shouldn't it fail instead?
P.S. If this question has already been asked, could someone link the original since I haven't been able to find it.
Edit: Added the debug information I mentioned in the comment section, so that we have everything in one place.
For the Timestamp
instances made from Instant.MIN
and Instant.MAX
, I had the following values:
past = 169108098-07-03 21:51:43.0
future = 169104627-12-11 11:08:15.999999999
present = 2018-07-23 10:46:50.842
and for the Timestamp
instances made from Long.MIN_VALUE
and Long.MAX_VALUE
, I got:
past = 292278994-08-16 23:12:55.192
future = 292278994-08-16 23:12:55.807
present = 2018-07-23 10:49:54.281
To clarify my question, instead of failing silently or using using a different value internally, the Timestamp should fail explicitly. Currently it doesn't.