我在服务器上使用MySQL数据库。我的Web应用程序服务器位于美国/芝加哥但我希望将我的DateTime值保存在非洲/拉各斯时间。Java - 使用SQL时间戳的TimeZone转换问题
我从Stack Overflow得到了这段代码的一部分,但是当我更新记录时,它仍然在数据库记录中显示芝加哥时间。 请问我在这里做错了什么?
public static boolean saveImageFileName(int id, String fileName) throws SQLException, IOException, IllegalArgumentException, ClassNotFoundException
{
try(Connection conn = Config.getDatabaseConnection())
{
//Create a timezone object based on Africa/Lagos time zone
SimpleTimeZone timeZone = new SimpleTimeZone(1, "Africa/Lagos");
//Create a calendar object using the timezone object
GregorianCalendar calendar = new GregorianCalendar(timeZone);
//Create a timestamp object using the calendar's date and time
Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
//Create an SQL query String
String sql = "UPDATE `requests` SET `filename` = ?, `uploaded_date` = ? WHERE `id` = ?";
//Create a prepared statement object from database connection
PreparedStatement pst = conn.prepareStatement(sql);
//Set prepared statement parameters
pst.setString(1, fileName);
pst.setTimestamp(2, timestamp, calendar); //Set TimeStamp and Calendar
pst.setInt(3, id);
//Update the record
int update = pst.executeUpdate();
//return update result
return update == 1;
}
}
它仍然显示芝加哥时间2017-06-04 08:31:35而不是2017-06-04 14:31:35 – Jevison7x