[java-core] Timestamp.of(java.sql.Timestamp) is one second late for a time less than a second before the epoch

Open Beginner friendly
#14,636 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Vcode2407 is already working on this.

Since Nov 9, 2026.

  • #14948 by @Vcode2407 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
backend

Research direction

Start in sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java, at the Timestamp.of(java.sql.Timestamp) method linked in the issue. Compare its seconds calculation with Timestamp.of(Date) and the reproducer values around the epoch. Done means conversion is correct for the negative subsecond cases and agrees with Timestamp.of(Date); the issue does not name a test file.

Written by the indexing model from the issue text.

Description

Issue Details

Timestamp.of(java.sql.Timestamp) returns a time one second late for a java.sql.Timestamp between one second before the epoch and the epoch:

https://github.com/googleapis/google-cloud-java/blob/9de611fe5633e9f43c6081754c834cabcd0317bb/sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/Timestamp.java#L123-L136

The method takes the whole seconds as getTime() / 1000 and borrows a second only when that quotient is negative. For getTime() in (-1000, 0) the quotient is 0, so no second is borrowed, while getNanos() holds the positive fraction of the second before the epoch. new java.sql.Timestamp(-500) (1969-12-31T23:59:59.500Z) becomes 1970-01-01T00:00:00.500Z. The borrow was added for googleapis/java-core#159, whose example (-1001) has a nonzero quotient, so this range was left out. Math.floorDiv(getTime(), 1000) gives the right seconds for every value, as Timestamp.of(Date) already does through ofTimeMicroseconds.

Found while building an Apache Flink connector on google-cloud-firestore (flink-gcp/flink-connector-gcp).

Environment

  • OS Type and Version: macOS 26 (arm64)
  • Java Version and JDK Vendor: Eclipse Temurin 17.0.20

Dependencies

  • Libraries-Bom: 26.90.0
    • google-cloud-core 2.77.0

The affected code is identical in 2.77.0 and on main.

Reproducer

package repro;

import com.google.cloud.Timestamp;

public class SqlTimestampProbe {
  public static void main(String[] args) {
    for (long millis : new long[] {-1500, -500, -1, 0, 500}) {
      java.sql.Timestamp sql = new java.sql.Timestamp(millis);
      System.out.println(millis + " ms (" + sql.toInstant() + "): Timestamp.of(sql) = " + Timestamp.of(sql)
          + ", Timestamp.of(Date) = " + Timestamp.of(new java.util.Date(millis)));
    }
  }
}

Logs and Stack Trace

-1500 ms (1969-12-31T23:59:58.500Z): Timestamp.of(sql) = 1969-12-31T23:59:58.500000000Z, Timestamp.of(Date) = 1969-12-31T23:59:58.500000000Z
-500 ms (1969-12-31T23:59:59.500Z): Timestamp.of(sql) = 1970-01-01T00:00:00.500000000Z, Timestamp.of(Date) = 1969-12-31T23:59:59.500000000Z
-1 ms (1969-12-31T23:59:59.999Z): Timestamp.of(sql) = 1970-01-01T00:00:00.999000000Z, Timestamp.of(Date) = 1969-12-31T23:59:59.999000000Z
0 ms (1970-01-01T00:00:00Z): Timestamp.of(sql) = 1970-01-01T00:00:00Z, Timestamp.of(Date) = 1970-01-01T00:00:00Z
500 ms (1970-01-01T00:00:00.500Z): Timestamp.of(sql) = 1970-01-01T00:00:00.500000000Z, Timestamp.of(Date) = 1970-01-01T00:00:00.500000000Z

Behavior

Deterministic: any java.sql.Timestamp with -1000 < getTime() < 0.

Dominant language
Java
Stars
2.1k
Forks
1.2k
Avg merge
2d 14h
Merged PRs (30d)
192

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from googleapis/google-cloud-java

All issues in googleapis/google-cloud-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.