我在那裏我存儲從傳感器MySQL的選擇日期時間不夏令時的變化
CREATE TABLE `testdatabase` (
`dateTime` datetime DEFAULT NULL,
`data` varchar(200) DEFAULT NULL,
`sensorID` varchar(10) DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6918 DEFAULT CHARSET=utf8
這是我運行的是選擇查詢的數據表,
SELECT `dateTime` from testdatabase WHERE sensorID='ABC1234' AND (dateTime BETWEEN '2017-03-26 00:01:00' AND '2017-03-26 00:00:00') order by `dateTime` asc;
在MySQL工作臺,當我運行此查詢,它會返回正確的日期時間。
2017-03-26 00:10:00
2017-03-26 00:20:00
2017-03-26 00:30:00
2017-03-26 00:40:00
2017-03-26 00:50:00
2017-03-26 01:00:00
2017-03-26 01:10:00
2017-03-26 01:20:00
2017-03-26 01:30:00
2017-03-26 01:40:00
2017-03-26 01:50:00
2017-03-26 02:00:00
2017-03-26 02:10:00
2017-03-26 02:20:00
2017-03-26 02:30:00
2017-03-26 02:40:00
2017-03-26 02:50:00
2017-03-26 03:00:00
但是,當我從我的Java應用程序運行此查詢時,它會返回以下日期時間。
2017-03-26 00:10:00
2017-03-26 00:20:00
2017-03-26 00:30:00
2017-03-26 00:40:00
2017-03-26 00:50:00
2017-03-26 02:00:00
2017-03-26 02:10:00
2017-03-26 02:20:00
2017-03-26 02:30:00
2017-03-26 02:40:00
2017-03-26 02:50:00
2017-03-26 02:00:00
2017-03-26 02:10:00
2017-03-26 02:20:00
2017-03-26 02:30:00
2017-03-26 02:40:00
2017-03-26 02:50:00
2017-03-26 03:00:00
很明顯,這與英國發生在3月26日凌晨1點的DST變化有關。 我認爲JDBC連接正在改變BST的時間,但我不希望這樣。
我試圖MySQL服務器全球時區設置爲00:00
SET @@global.time_zone='+00:00';
但沒有成功! 如何在沒有DST更改的情況下獲得正確的時間?
編輯: 我使用Java 1.7
和mysql-connector-java-5.1.19-bin.jar
我得到的數據爲字符串之前,但之後Mark Rotteveel
評論我試着PreparedStaement
設置爲UTC日曆對象,但都返回相同的結果。
try
{
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next())
{
System.out.println("Without Calendar Object : "+resultSet.getString(1));
}
}catch (SQLException e){e.printStackTrace();}
try
{
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
while(resultSet.next())
{
System.out.println("With Calendar Object : "+resultSet.getTimestamp(1, cal));
}
}catch (SQLException e){e.printStackTrace();}
OUTPUT:
Without Calendar Object : 2017-03-26 00:10:00.0
Without Calendar Object : 2017-03-26 00:20:00.0
Without Calendar Object : 2017-03-26 00:30:00.0
Without Calendar Object : 2017-03-26 00:40:00.0
Without Calendar Object : 2017-03-26 00:50:00.0
Without Calendar Object : 2017-03-26 02:00:00.0
Without Calendar Object : 2017-03-26 02:10:00.0
Without Calendar Object : 2017-03-26 02:20:00.0
Without Calendar Object : 2017-03-26 02:30:00.0
Without Calendar Object : 2017-03-26 02:40:00.0
Without Calendar Object : 2017-03-26 02:50:00.0
Without Calendar Object : 2017-03-26 02:00:00.0
Without Calendar Object : 2017-03-26 02:10:00.0
Without Calendar Object : 2017-03-26 02:20:00.0
Without Calendar Object : 2017-03-26 02:30:00.0
Without Calendar Object : 2017-03-26 02:40:00.0
Without Calendar Object : 2017-03-26 02:50:00.0
Without Calendar Object : 2017-03-26 03:00:00.0
With Calendar Object : 2017-03-26 00:10:00.0
With Calendar Object : 2017-03-26 00:20:00.0
With Calendar Object : 2017-03-26 00:30:00.0
With Calendar Object : 2017-03-26 00:40:00.0
With Calendar Object : 2017-03-26 00:50:00.0
With Calendar Object : 2017-03-26 02:00:00.0
With Calendar Object : 2017-03-26 02:10:00.0
With Calendar Object : 2017-03-26 02:20:00.0
With Calendar Object : 2017-03-26 02:30:00.0
With Calendar Object : 2017-03-26 02:40:00.0
With Calendar Object : 2017-03-26 02:50:00.0
With Calendar Object : 2017-03-26 02:00:00.0
With Calendar Object : 2017-03-26 02:10:00.0
With Calendar Object : 2017-03-26 02:20:00.0
With Calendar Object : 2017-03-26 02:30:00.0
With Calendar Object : 2017-03-26 02:40:00.0
With Calendar Object : 2017-03-26 02:50:00.0
With Calendar Object : 2017-03-26 03:00:00.0
編輯2: 有,當我更改默認的時區爲UTC它凌晨1點,而不是凌晨2時返回重複值,另一個奇怪的事情。
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
OUTPUT:
Without Calendar Object : 2017-03-26 00:10:00.0
Without Calendar Object : 2017-03-26 00:20:00.0
Without Calendar Object : 2017-03-26 00:30:00.0
Without Calendar Object : 2017-03-26 00:40:00.0
Without Calendar Object : 2017-03-26 00:50:00.0
Without Calendar Object : 2017-03-26 01:00:00.0
Without Calendar Object : 2017-03-26 01:10:00.0
Without Calendar Object : 2017-03-26 01:20:00.0
Without Calendar Object : 2017-03-26 01:30:00.0
Without Calendar Object : 2017-03-26 01:40:00.0
Without Calendar Object : 2017-03-26 01:50:00.0
Without Calendar Object : 2017-03-26 01:00:00.0
Without Calendar Object : 2017-03-26 01:10:00.0
Without Calendar Object : 2017-03-26 01:20:00.0
Without Calendar Object : 2017-03-26 01:30:00.0
Without Calendar Object : 2017-03-26 01:40:00.0
Without Calendar Object : 2017-03-26 01:50:00.0
Without Calendar Object : 2017-03-26 02:00:00.0
With Calendar Object : 2017-03-26 00:10:00.0
With Calendar Object : 2017-03-26 00:20:00.0
With Calendar Object : 2017-03-26 00:30:00.0
With Calendar Object : 2017-03-26 00:40:00.0
With Calendar Object : 2017-03-26 00:50:00.0
With Calendar Object : 2017-03-26 01:00:00.0
With Calendar Object : 2017-03-26 01:10:00.0
With Calendar Object : 2017-03-26 01:20:00.0
With Calendar Object : 2017-03-26 01:30:00.0
With Calendar Object : 2017-03-26 01:40:00.0
With Calendar Object : 2017-03-26 01:50:00.0
With Calendar Object : 2017-03-26 01:00:00.0
With Calendar Object : 2017-03-26 01:10:00.0
With Calendar Object : 2017-03-26 01:20:00.0
With Calendar Object : 2017-03-26 01:30:00.0
With Calendar Object : 2017-03-26 01:40:00.0
With Calendar Object : 2017-03-26 01:50:00.0
With Calendar Object : 2017-03-26 02:00:00.0
查看http://stackoverflow.com/questions/14070572/is-java-sql-timestamp-timezone-specific/14070771#14070771 –
@MarkRotteveel在這個問題中,數據類型是時間戳,但我的表有一個日期時間的數據類型,我的印象是隻有時間戳存儲時區值,日期時間不存儲此信息 – Ana
不要將JDBC數據類型與MySQL數據類型混淆。 'Types.TIMESTAMP'(或'java.sql.Timestamp')沒有時區,但是當從數據庫中檢索一個沒有時區信息的數據類型的值時(例如MySQLs'datetime'),JDBC需要它會在當前的JVM時區中解釋,除非您使用'getTimestamp'來獲取帶有時區信息的'Calendar'對象。 –