pgjdbc/pgjdbc GitHub issues and pull requests (mirror)  
help / color / mirror / Atom feed
From: michalkoza (@michalkoza) <[email protected]>
To: pgjdbc/pgjdbc <[email protected]>
Subject: Re: [pgjdbc/pgjdbc] issue #1325: Additional jsr 310 support
Date: Wed, 07 Aug 2024 21:58:38 +0000
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

@marschall Conversion from java.sql.Timestamp -> Instant is independent of time zone. Check this code:
```java
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
LocalDateTime localDateTime = LocalDateTime.of(2018, 11, 16, 18, 2);
Timestamp timestamp = Timestamp.valueOf(localDateTime);
Instant instant1 = timestamp.toInstant();
System.out.println("timestamp in LA zone " + timestamp);
System.out.println("instant1  in LA zone " + instant1);

TimeZone.setDefault(TimeZone.getTimeZone("America/New_York"));
Instant instant2 = timestamp.toInstant();
System.out.println("timestamp in NY zone " + timestamp);
System.out.println("instant1  in NY zone " + instant1);
System.out.println("instant2  in NY zone " + instant2);
long epochMilli1 = instant1.toEpochMilli();
long epochMilli2 = instant2.toEpochMilli();
System.out.printf("epochMilli1: %d%n", epochMilli1);
System.out.printf("epochMilli2: %d%n", epochMilli2);
System.out.println("same instant: " + instant1.equals(instant2));
```

If you have a fixed timestamp, you can change time zones and convert it to Instant many times, always getting the same result. Observe also that even though the `timestamp` is never changed it is displayed differently depending on time zone, but it keeps the same value.

In your example the difference comes from the fact that 18:02 in LA is different moment in time than 18:02 in NY. It would be surprising if your Instants and Timestamps would be equal.

`Instant` is a moment in time in UTC. The same goes for `TIMESTAMP` type in PostgreSQL. They are both timezone-agnostic. So I struggle to understand why would you not want to store Instant as TIMESTAMP rather than TIMESTAMPTZ

view thread (25+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: github://pgjdbc/pgjdbc
  Cc: [email protected], [email protected]
  Subject: Re: [pgjdbc/pgjdbc] issue #1325: Additional jsr 310 support
  In-Reply-To: <<[email protected]>>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox