2017-02-27 200 views
0

我正在使用Postgresql JDBC爲了從另一個服務器讀取與我的客戶端系統有不同時區的數據。Postgresql JDBC讀取時區數據

讀取數據有一些日期類型列。我希望這些列成爲客戶端系統時區,爲了做到這一點,我認爲應該有一種方法可以告訴JDBC,服務器的時區是什麼。

我用pypsark使用JDBC。

任何幫助?謝謝。

回答

0

timestamptz當您檢索數據時,數據將自動轉換爲Java默認時區。

您可以使用java.util.TimeZone.setDefault(java.util.TimeZone)更改此默認值。

由於其設置爲整個JVM,這是非常侵入性的時間段,我會告訴你如何將時間戳格式到一定的時區:

java.sql.ResultSet rs = stmt.executeQuery(); 
rs.next(); 
// fetch the first result column as java.sql.Timestamp object 
java.sql.Timestamp d = rs.getObject(1, java.sql.Timestamp.class); 
// create a calendar with the appropriate time zone 
java.util.Calendar cal = java.util.Calendar.getInstance(
     java.util.TimeZone.getTimeZone("Asia/Kolkata")); 
// create a DateFormat with the appropriate format and locale 
java.text.DateFormat df = java.text.DateFormat.getDateTimeInstance(
     java.text.DateFormat.MEDIUM, 
     java.text.DateFormat.MEDIUM, 
     java.util.Locale.ENGLISH); 
// tell the DateFormat to use the calendar with the right time zone 
df.setCalendar(cal); 
// output will be in the chosen time zone 
System.out.println("timestamp = " + df.format(d)); 
+1

使用'TimeZone.setDefault'可能不是一個好主意,它可以搞砸你的應用程序的其他部分。 –

+0

問題*這些列成爲客戶端系統時區*並且想知道如何設置它。但是我可以添加如何將數據轉換爲特定時區的信息。 –