(tomcat8, jdk8, 5.5.41-MariaDB, mysql-connector-java-5.1.40-bin.jar, windows10)
I have this DATETIME value in a table, webapp client and db server in a same machine using an Europe/Helsinki system timezone.
DST +1h was applied at 2017-03-26 02:59:59 EET -> 2017-03-26 04:00:00 EEST. My database table has a value: 2017-03-26 03:54:03
HeidiSQL and mysql.exe consoles display a datetime value as 2017-03-26 03:54:03. It's a correct UTC value, wallclock would be 2017-03-26 06:54:03 value.
Mysql query select @@time_zone, @@system_time_zone
gives SYSTEM,FLE Daylight Time result.
Jdbc resultset gives a value as 2017-03-26 04:54:03, this happens using a rs.getString("created_utc")
or rs.getTimestamp("created_utc")
getters. Converting this value to a wallclock is 2017-03-26 07:54:03 which is correct but was expecting to get original 03:XX:XX not 04:XX:XX stamp.
Also this one gives 2017-03-26 04:54:03 value but was expecting to have 03:54:03 time part.
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
java.sql.Timestamp ts = rs.getTimestamp("created_utc", cal);
Other hour stamps are fine but just this 03:XX:XX hourstamp is returned +1h too much. So jdbc value 04:01:00 to wallclock 07:01:00 works as expected. I would have guess all the timestamps would be one hour wrong but they are not. 00:XX:XX-02:XX:XX and 04:XX:XX-23:XX:XX works as expected.
I was able to fix this using an ugly hack but sure this cannot be the final answer?
byte[] buf=rs.getBlob("created_utc").getBytes(1, (int)rs.getBlob("created_utc").length());
String sval = new String(buf, "ISO-8859-1"); // "2017-03-26 03:54:03" utc string
Calendar cal = DateUtil.fromUTC(sval); // "2017-03-26 06:54:03" system timezone
EDIT This was marked as a duplicate so I edit here my solution, problem was solved using a linked topic and comments in this question.
- Use
jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&useLegacyDatetimeCode=false
jdbc string - Use
rs.getTimestamp("started_utc", sqlUTC)
getter for datetime field