I have a table where I store data from the sensors
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
This is the select query I am running,
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;
In MySQL workbench when I run this query, it returns the correct date time.
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
But when I run this query from my java app, it returns the following date time.
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
Obviously this has something to do with the DST change in the UK which happened on 26th March at 1am. I think JDBC connection is changing the time to BST but I don't want that.
I have tried to set the MySQL server global time zone to 00:00
SET @@global.time_zone='+00:00';
But no success! How can I get the correct time without the DST change?
EDIT:
I am using Java 1.7
and mysql-connector-java-5.1.19-bin.jar
I was getting data as strings before but after Mark Rotteveel
comments I tried PreparedStaement
with the calendar object set to UTC but both returning the same results.
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
EDIT 2: There is another strange thing that when I change the default timezone to UTC it returns repeating values for 1am instead of 2am.
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